简体   繁体   English

在utf-8中使用csv.reader打开文件

[英]open file with csv.reader in utf-8

i try to open a file in utf-8 and step trough the file with the csv.reader: 我尝试在utf-8中打开一个文件,然后使用csv.reader一步浏览该文件:

    with codecs.open(sap_file, "rb", "utf-8") as in_file:
        #self.logger.debug("open")
        self.reader = csv.reader(in_file,delimiter=";")
        for row in self.reader:
            self.pnrs[(row[1])]={}
        for rows in self.reader:
            self.pnrs[rows[1]][rows[3]]=rows[4]

But here is my traceback: 但是这是我的回溯:

for row in self.reader:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 38:  ordinal not in range(128)

Somebody here with some tips? 有人在这里有一些提示吗?

I think you are opening the file in the wrong way, try open(sap_file, "rb") instead of codecs.open(sap_file, "rb", "utf-8") . 我认为您打开文件的方式有误,请尝试使用open(sap_file, "rb")而不是codecs.open(sap_file, "rb", "utf-8")

Also read here . 也可以在这里阅读。

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

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