我正在閱讀編碼爲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