我一直在尝试导入包含特殊字符的csv文件(äöü)
在python 2.x中自动编码的所有特殊字符,无需在open命令中指定 econding 属性。
我无法弄清楚如何在 python 3.x
中使用它import csv
f = open('sample_1.csv', 'rU', encoding='utf-8')
csv_f = csv.reader(f, delimiter=';')
bla = list(csv_f)
print(type(bla))
print(bla[0])
print(bla[1])
print(bla[2])
print()
print(bla[3])
控制台输出(Sublime Build python3)
<class 'list'>
['\ufeffCat1', 'SEO Meta Text']
['Damen', 'Damen----']
['Damen', 'Damen-Accessoires-Beauty-Geschenk-Sets-']
Traceback (most recent call last):
File "/Users/xxx/importer_tree.py", line 13, in <module>
print(bla[3])
UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 37: ordinal not in range(128)
输入sample_1.csv (excel文件保存为utf-8 csv)
Cat1;SEO Meta Text
Damen;Damen----
Damen;Damen-Accessoires-Beauty-Geschenk-Sets-
Damen;Damen-Accessoires-Beauty-Körperpflege-
Männer;Männer-Sport-Sportschuhe-Trekkingsandalen-
Männer;Männer-Sport-Sportschuhe-Wanderschuhe-
Männer;Männer-Sport-Sportschuhe--
感谢您的帮助/指导!
答案 0 :(得分:0)
感谢juanpa-arrivillaga和这个回答:https://stackoverflow.com/a/44088439/9059135
问题是由于我的Sublime设置:
sys.stdout.encoding
返回 US-ASCII
在终端中同一命令返回 UTF-8
正确设置Build system in Sublime将解决问题