简体   繁体   English

编码/解码Unicode和编写CSV

[英]Encoding/Decoding Unicode and Writing CSV

I am trying to write words of non-latin based languages to a CSV and cannot get the words to be written in their proper form. 我正在尝试将非拉丁语言的单词写到CSV,但是无法以正确的形式写出单词。

foreign='а также'
with open('C:\\Users\\Lance\\Desktop\\Programs\\Database Builder\\Russian Test.csv', 'wb') as outfile:
    outfile.write((foreign).encode('utf-8'))

The output of this code is: 此代码的输出是:

а также атак涵µ

Thanks! 谢谢!

It writes the file correctly but you are probably displaying the file using an editor or console that is using Windows-1252 encoding. 它可以正确写入文件,但是您可能正在使用使用Windows-1252编码的编辑器或控制台来显示文件。

Example from US Windows cmd.exe console: 来自美国Windows cmd.exe控制台的示例:

C:\>type "Russian Test.csv"
а также
C:\>chcp 1252
Active code page: 1252

C:\>type "Russian Test.csv"
а также
C:\>chcp 65001
Active code page: 65001

C:\>type "Russian Test.csv"
а также

Note: code page 65001 is UTF-8 encoding on Windows. 注意:代码页65001在Windows上是UTF-8编码。

Since you seem to be using Python 3, you should do this instead and write Unicode strings directly: 由于您似乎正在使用Python 3,因此应该改而直接写Unicode字符串:

foreign='а также'
with open('Russian Test.csv', 'w', encoding='utf8') as outfile:
    outfile.write(foreign)

First install unicodecsv 首次安装unicodecsv

pip install unicodecsv

Then import it in your script 然后将其导入您的脚本中

import unicodecsv as csv

Worked for me. 为我工作。

firstly, writing data into csv file depends on csv library, the correct script should be: 首先,将数据写入csv文件取决于csv库,正确的脚本应为:

import csv
with open('path/to/test.csv', 'wb') as f:
    writer = csv.writer(f)
    for line in <your_data>:
       writer.writerow(line)

secondly, as csv library does not support unicode in python 2x, you need use alternative that handle unicode very well -- https://github.com/jdunck/python-unicodecsv , All you have to do is simply installing unicode version of csv library, and adding short import expression at first line: 其次,由于csv库在python 2x中不支持unicode,因此您需要使用能够很好地处理unicode的替代方法-https: //github.com/jdunck/python-unicodecsv ,您要做的就是简单地安装Unicode版本的csv库,并在第一行添加简短的导入表达式:

pip install unicodecsv
import unicodecsv as csv
...

Remember that convert all your strings into unicode by adding 'u' in front of each string. 请记住,通过在每个字符串前面添加“ u”,将所有字符串转换为unicode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM