我正在尝试使用 utf-8 文本格式加载 .csv 文件,并将其写入带有管道分隔符的 cp1252(ansi) 格式。以下代码在 Python 3.6 中工作,但我需要它在 Python 2.6 中工作。但是,'open' 函数在 Python 2.6 中不允许使用编码关键字。
import datetime
import csv
# Define what filenames to read
filenames = ["FILE1","FILE2"]
infilenames = [filename+".csv" for filename in filenames]
outfilenames = [filename+"_out_.csv" for filename in filenames]
# Read filenames in utf-8 and write them in cp1252
for infilename,outfilename in zip(infilenames,outfilenames):
infile = open(infilename, "rt",encoding="utf8")
reader = csv.reader(infile,delimiter=',',quotechar='"',quoting=csv.QUOTE_MINIMAL)
outfile = open(outfilename, "wt",encoding="cp1252")
writer = csv.writer(outfile, delimiter='|', quotechar='"', quoting=csv.QUOTE_NONE,escapechar='\\')
for row in reader:
writer.writerow(row)
infile.close()
outfile.close()
我尝试了几种解决方案:
- 没有定义编码。导致某些 unicode 字符出错
- 使用 io 库(io.open 而不是 open)。导致“类型错误:无法将 str 写入文本流中的文本”。
有谁知道 Python 2.X 中的正确解决方案?