【发布时间】:2017-01-23 18:11:21
【问题描述】:
我正在阅读.csv,它是UTF-8 编码的。
我想创建一个索引并重写csv。
索引被创建为一个持续的数字和一个单词的第一个字母。
Python 2.7.10,Ubuntu 服务器
#!/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 Server -
@JoshLee 在
Ubuntu Server上工作,然后我将文件下载到 Windows 计算机上。但是,当我检查Ubuntu Server上的文件时,该错误已经可见 -
哦不! Sublime text 似乎用问号替换无效字节而没有任何警告或错误,这至少可以说是令人震惊的。