我需要比较Unicode字符串从在Python脚本定义一个常数的UTF-8的文件来。
我在Linux上使用的Python 2.7.6。
如果我运行中的Spyder(一个Python编辑)上面的脚本我得到了它的工作,但如果我从终端调用Python脚本,我得到了测试失败。 我是否需要导入/调用脚本之前定义在终端的东西吗?
脚本( “pythonscript.py”):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
some_french_deps = []
idata_raw = csv.DictReader(open("utf8_encoded_data.csv", 'rb'), delimiter=";")
for rec in idata_raw:
depname = unicode(rec['DEP'],'utf-8')
some_french_deps.append(depname)
test1 = "Tarn"
test2 = "Rhône-Alpes"
if test1==some_french_deps[0]:
print "Tarn test passed"
else:
print "Tarn test failed"
if test2==some_french_deps[2]:
print "Rhône-Alpes test passed"
else:
print "Rhône-Alpes test failed"
utf8_encoded_data.csv:
DEP
Tarn
Lozère
Rhône-Alpes
Aude
从运行Spyder的编辑器输出:
Tarn test passed
Rhône-Alpes test passed
运行从终端输出:
$ ./pythonscript.py
Tarn test passed
./pythonscript.py:20: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
if test2==some_french_deps[2]:
Rhône-Alpes test failed