我制作了一个Python脚本,用于从.csv存档中获取信息,并将其作为列表输出到文本文件中。原始csv文件有超过200,000个字段用于输入和输出,但是当我运行我的程序时,它只输出36到.txt文件中。
以下是代码:
import csv
with open('OriginalFile.csv', 'r') as csvfile:
emailreader = csv.reader(csvfile)
f = open('text.txt', 'a')
for row in emailreader:
f.write(row[1] + "\n")
文本文件最多只能列出36个字符串。我怎样才能解决这个问题?也许原来的csv文件太大了?
答案 0 :(得分:0)
经过多次评论后,原来的问题是csv文件中的字符编码。如果你在pandas中指定编码,它会很好地读取它。
每当你处理csv文件(或excel,sql或R)时,我都会使用Pandas DataFrames。语法更短,更容易知道发生了什么。
import pandas as pd
csvframe = pd.read_csv('OriginalFile.csv', encoding='utf-8')
with open('text.txt', 'a') as output:
# I think what you wanted was the 2nd column from each row
output.write('\n'.join(csvframe.ix[:,1].values))
# the ix is for index and : is for all the rows and the 1 is only the first column
答案 1 :(得分:-1)
您可能会幸运地获得以下内容:
with open('OriginalFile.csv', 'r') as csvfile:
emailreader = csv.reader(csvfile)
with open('text.txt','w') as output:
for line in emailreader:
output.write(line[1]+'\n')