import csv
with open('eggs.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
test_jp = u'\u30a2\u30af\u30bb\u30b5\u30ea\u30fc'
print(test_jp)
print(type(test_jp))
print(repr(test_jp))
print('-------------------')
print(test_jp.encode('utf-8'))
print(test_jp.encode('cp932'))
'''
print(test_jp.decode('utf-8'))
spamwriter.writerow(test_jp)
Causeing ERROR
UnicodeEncodeError: 'ascii' codec can't encode characters
in position 0-5: ordinal not in range(128)
'''
我尝试过spamwriter.writerow(test_jp.encode('utf-8'))。
但输出是乱码 - > “ã,¢ã,¯ã,»ã,μリー' 。
我想输出的csv内容是'アクセサリー'
我该怎么办? (spamwriter.writerow(test_jp)不起作用)
答案 0 :(得分:0)
你需要将它包装在list
for writerow然后test_jp.encode("utf-8")
将起作用,写作者期望一个可迭代的,所以它迭代遍历字符串写每个字节:
spamwriter.writerow([test_jp.encode("utf-8")])
你可以看到,当我们迭代它时,我们也会得到奇怪的输出:
In [6]: for ch in test_jp.encode("utf-8"):
print ch
...:
�
�
�
�
�
�
�
�
�
�
�
�
In [7]: print test_jp.encode("utf-8")
アクセサリー
经过测试和工作:
$ cat eggs.csv
Spam |Lovely Spam| |Wonderful Spam|
アクセサリー