简体   繁体   English

来自CSV文件的Python 2.7编码

[英]Python 2.7 encoding from csv file

I have a problem with Python 2.7 encoding I have a csv file with some french characters (mangé, parlé, prêtre ...), the code I'm using is the following: 我的Python 2.7编码有问题,我有一个带有某些法语字符(mangé,parlé,prêtre...)的csv文件,我使用的代码如下:

import pandas as pd
path_dataset = 'P:\\Version_python\\Dataset\\data_set - Copy.csv'
dataset = pd.read_csv(path_dataset, sep=';')

for lab, row in dataset.iterrows():
    print(row['Summary'])

I tried to add encoding to read_csv() , it didn't work. 我试图将encoding添加到read_csv() ,但是没有用。 I tried unicode , decode (UTF-8) ... Nothing worked. 我尝试了unicodedecode (UTF-8)...没有任何效果。

Then I tried to concatenate those extracted words with some text, and I got a utf-8 error, I don't know how to deal with that. 然后,我尝试将提取的单词与一些文本连接起来,然后出现utf-8错误,我不知道该如何处理。 Thanks 谢谢

Here is a list of standard python encodings 这是标准python编码的列表

Standard Python 2.7 encodings 标准Python 2.7编码

utf-8 does not work but you can try some other encodings on the link above. utf-8不起作用,但是您可以在上面的链接中尝试其他编码。

Just tested latin_1 works. 刚刚测试过的latin_1可以工作。 So the code should be: 因此,代码应为:

dataset = pd.read_csv(path_dataset, sep=';', encoding='latin_1')

You can use codecs in python2.7 您可以在python2.7中使用编解码器

import codecs
file = codecs.open(filename, encoding="utf-8")

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

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