我正在开发一个程序,需要获取两个文件并将它们合并并将联合文件写入新文件。问题是输出文件包含类似 \xf0
的字符,或者如果我更改某些编码,结果会类似于 \u0028
。输入文件采用 utf8 编码。我如何在输出文件上打印 "è"
或 "ò"
和 "-"
我已经完成了这段代码:
import codecs
import pandas as pd
import numpy as np
goldstandard = "..\\files\file1.csv"
tweets = "..\\files\\file2.csv"
with codecs.open(tweets, "r", encoding="utf8") as t:
tFile = pd.read_csv(t, delimiter="\t",
names=['ID', 'Tweet'],
quoting=3)
IDs = tFile['ID']
tweets = tFile['Tweet']
dict = {}
for i in range(len(IDs)):
dict[np.int64(IDs[i])] = [str(tweets[i])]
with codecs.open(goldstandard, "r", encoding="utf8") as gs:
for line in gs:
columns = line.split("\t")
index = np.int64(columns[0])
rowValue = dict[index]
rowValue.append([columns[1], columns[2], columns[3], columns[5]])
dict[index] = rowValue
import pprint
pprint.pprint(dict)
ndic = pprint.pformat(dict, indent=4)
f = codecs.open("out.csv", "w", "utf8")
f.write(ndic)
f.close()
这是输出的示例
desired: Beyoncè
obtained: Beyonc\xe9
最佳答案
您正在生成 Python 字符串文字,此处:
import pprint
pprint.pprint(dict)
ndic = pprint.pformat(dict, indent=4)
pretty-print 对于生成调试输出很有用;对象通过 repr()
传递,使不可打印和非 ASCII 字符易于区分和重现:
>>> import pprint
>>> value = u'Beyonc\xe9'
>>> value
u'Beyonc\xe9'
>>> print value
Beyoncé
>>> pprint.pprint(value)
u'Beyonc\xe9'
é
字符位于 Latin-1 范围内,位于 ASCII 范围之外,因此用在 Python 代码中使用时再次生成相同值的语法表示。
如果您想将实际字符串值写入输出文件,请不要使用pprint
。在这种情况下,您必须自己进行格式化。
此外,pandas 数据帧将保存字节串,而不是 unicode
对象,因此此时您仍然拥有未解码的 UTF-8 数据。
就我个人而言,我什至懒得在这里使用 pandas;您似乎想要写入 CSV 数据,因此我简化了您的代码以使用 csv
模块,而且我实际上并不想解码 UTF-8这里(对于这种情况来说这是安全的,因为输入和输出都完全采用 UTF-8):
import csv
tweets = {}
with open(tweets, "rb") as t:
reader = csv.reader(t, delimiter='\t')
for id_, tweet in reader:
tweets[id_] = tweet
with open(goldstandard, "rb") as gs, open("out.csv", 'wb') as outf:
reader = csv.reader(gs, delimiter='\t')
writer = csv.reader(outf, delimiter='\t')
for columns in reader:
index = columns[0]
writer.writerow([tweets[index]] + columns[1:4] + [columns[5])
请注意,您确实希望避免使用 dict
作为变量名;它掩盖了内置类型,我使用 tweets
代替。
关于Python关于字符编码的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36923413/