【发布时间】:2014-12-10 21:08:01
【问题描述】:
在 Python 3.4 中从 Python shell 运行这段代码就可以了:
from csv import reader
if __name__ == '__main__':
filename = "valid.csv"
with open(filename) as f:
csvreader = reader(f)
for keyword in csvreader:
kw = keyword[0]
但是,在 Eclipse 中使用 Pydev 3.8 或 3.9 运行它会失败:
pydev debugger: starting (pid: 64416)
Traceback (most recent call last):
File "/Applications/eclipse/plugins/org.python.pydev_3.9.0.201411111611/pysrc/pydevd.py", line 2183, in <module>
globals = debugger.run(setup['file'], None, None)
File "/Applications/eclipse/plugins/org.python.pydev_3.9.0.201411111611/pysrc/pydevd.py", line 1622, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/eclipse/plugins/org.python.pydev_3.9.0.201411111611/pysrc/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/antoinebrunel/src/test_pydev/test_pydev/test_pydev.py", line 14, in <module>
for keyword in csvreader:
File "/Users/antoinebrunel/.virtualenvs/seo3/lib/python3.4/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)
为了让代码运行,替换:
with open(filename, mode='rt') as f:
与:
with open(filename, mode='rt', encoding='utf-8') as f:
注意 1:文件 valid.csv 包含任何带重音的字符,例如:
à
注2:在Eclipse 首选项> 工作区中,文本文件编码设置为utf-8
参考 This StackOverflow question 可能相关
【问题讨论】:
-
你是说你已经解决了这个问题吗?如果是这样,请将答案部分放在下面的答案帖子中。还是这里还有一个实际问题?
-
这还是个问题,还没有解决,Pydev Brainwy tracker 上也开了一张 #497 的票,显然这将在下一个版本中解决。
标签: python python-3.x pydev