使用utf-8编码/解码时出现问题

时间:2017-01-23 18:11:22

标签: python csv encoding utf-8

我正在阅读.csv UTF-8编码的csv。 我想创建一个索引并重写#!/usr/bin/env python # -*- coding: utf-8 -*- counter = 0 tempDict = {} with open(modifiedFile, "wb") as newFile: with open(originalFile, "r") as file: for row in file: myList = row.split(",") toId = str(myList[0]) if toId not in tempDict: tempDict[toId] = counter myId = str(toId[0]) + str(counter) myList.append(myId) counter += 1 else: myId = str(toId[0]) + str(tempDict[toId]) myList.append(myId) # and then I write everything into the csv for i, j in enumerate(myList): if i < 6: newFile.write(str(j).strip()) newFile.write(",") else: newFile.write(str(j).strip()) newFile.write("\n") 。 索引创建为正在进行的数字一个单词的第一个字母。 Python 2.7.10,Ubuntu Server

?

问题如下。 当一个单词以花哨的字母开头时,例如

  • C
  • É
  • A
  • ...

我创建的ID以csv开头,但不是以单词的字母开头。 奇怪的是,在我创建?的情况下,带有花哨字母的单词写得正确。没有isEmpty()或其他符号表示编码错误。

为什么?

2 个答案:

答案 0 :(得分:3)

除非您需要specific旧版C扩展,否则您不应该学习Python 2。

Python 3对unicode / bytes处理进行了重大更改,删除(大多数)隐式行为并使错误可见。使用open('filename', encoding='utf-8')仍然是一种很好的做法,因为默认编码取决于环境和平台。

确实,在Python 3中运行程序应该修复它而不做任何更改。但这就是你的错误所在:

        toId = str(myList[0])

这是一项禁止操作,因为myList[0]已经是str

            myId = str(toId[0]) + str(counter)

这是一个错误:toId是包含UTF-8数据的str(字节字符串)。除了一次处理一个字符外,你永远不想对UTF-8数据做任何事情。

with open(originalFile, "r") as file:

这是样式错误,因为它会掩盖内置函数file

在Python 2下进行此操作有两个更改。

  1. open(filename, mode)更改为io.open(filename, mode, encoding='utf-8')
  2. 停止在字符串上调用str(),因为它实际上会尝试对它们进行编码(用ASCII格式!)。
  3. 但你真的应该切换到Python 3。

    2.6和2.7新增了一些旨在将差距缩小到3的部分,其中一个是io模块,它以所有新的方式运行:Unicode文件和通用换行符

    ~$ python2.7 -c 'import io,sys;print(list(io.open(sys.argv[1],encoding="u8")))' <(printf $'\xc3\x84\r\n\xc3\xb9\r\n')
    [u'\xc4\n', u'\xf9\n']
    ~$ python3 -c 'import sys;print(list(open(sys.argv[1],encoding="u8")))' <(printf $'\xc3\x84\r\n\xc3\xb9\r\n')
    ['Ä\n', 'ù\n']
    

    这对于编写2和3的软件非常有用。同样,编码参数是可选的,但在所有平台上,默认编码都是依赖于环境的,因此最好具体。

答案 1 :(得分:0)

在python 2.x中,字符串默认为非unicode - str()返回非unicode字符串。请改用unicode()

此外,您必须使用codecs.open()而不是内置的open()使用utf-8编码打开文件。