我正在阅读编码为UTF-8
的.csv
。 我想创建一个索引并重写csv
。 索引创建为一个正在进行的数字和单词的第一个字母。 Python 2.7.10,Ubuntu服务器UTF-8编码/解码的问题
#!/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")
问题是以下内容。 当一个单词以一个奇特的字母开头,如
- Č
- É
- Â转
- ...
我开始创建一个?
的ID,但不与字的字母。 奇怪的部分是,与我创建的csv
一样,带有花哨字母的单词被写入正确。没有?
或其他表示错误编码的符号。
这是为什么?
你使用的是什么版本的Python? –
如果你在Windows上,它可能使用语言环境编码。 –
@TimMartin 2.7.10,在Ubuntu服务器上工作 – Stophface