尝试将多个csv文件读取并解析为txt文件。能够读取csv并将修改后的内容写入txt文件。但是,在使用原始csv检查输出时,发现第一条记录不是写入文本文件。
输入csv文件的示例
INDEX,STRING1,STRING2,NUMBER1,NUMER2,DATE
ABC,abc,bcd,123,321,01-FEB-2017
BCD,bcd,cde,231,432,01-FEB-2017
我的代码:
file_list =[]
list_of_files = os.listdir(os.getcwd())
for each_file in list_of_files: #reading csv files from the folder#
if (each_file.startswith('report') and each_file.endswith('.csv')):
file_list.append(each_file)
print(each_file) #all ok here#
for f in file_list: #reading from list of files above#
with open (f, 'r') as fin:
reader=csv.reader(fin)
next(reader, None)
for row in csv.reader(fin):
#reading date and creating txt file for that date#
file_date=datetime.datetime.strptime(row[5],'%d-%b-%Y').strftime('%Y%m%d')
text_file = open("Report_for_"+file_date+".txt", "w")
#writing header for the output file#
text_file.write("<field0>,<date>,<field2>,<field3>,..."+"\n")
for row in csv.reader(fin):
if row[1] == 'abc' or row[1] == 'xyz':
ASCII=row[0]+','+file_date+','+row[2]+','+row[3]+','+row[4]+','+row[1]
text_file.write(ASCII +"\n")
print ("Processing for " +file_date)
text_file.close()
print("done....")
我使用next(reader, None)
跳过标题。但我的代码也是从所有csv文件中跳过第一条记录。即使经过反复试验和错误也无法确定我的错误。
答案 0 :(得分:1)
您可以创建多个读者:
reader=csv.reader(fin)
next(reader, None)
for row in csv.reader(fin):
# ...
for row in csv.reader(fin):
Reader对象使用缓冲,每当您请求一行并且缓冲区运行不足时,就会从文件对象中读取更大的数据块。因此,第一个reader()
将数据拉入abuffer,然后当第二个csv.reader()
开始读取文件对象时,已将其转发到下一个块。这同样适用于您创建的第三个读者。
循环reader
,而不是csv.reader(fin)
:
reader=csv.reader(fin)
next(reader, None)
for row in reader:
# ...
for row in reader:
我不太清楚为什么你在这里使用两个循环。如果您想根据日期将写入数据拆分为单独的文件,请将打开的文件存储在字典中,然后可以从中选择传出文件。
您还希望使用csv.writer()
对象将行写入新文件,而不是使用逗号手动连接:
try:
open_writing_files = {}
writers = {}
for f in file_list:
with open (f, 'rb') as fin:
reader = csv.reader(fin)
next(reader, None)
for row in reader(fin):
if row[1] not in ('abc', 'xyz'):
# not interesting, continue to the next row
continue
file_date = datetime.datetime.strptime(row[5], '%d-%b-%Y').strftime('%Y%m%d')
writer = writers.get(file_date)
if writer is None:
# no writer for this date yet, create one
text_file = open("Report_for_{}.txt".format(file_date), "wb")
open_writing_files[file_date] = text_file
writer = writers[file_date] = csv.writer(text_file)
writer.writerow(['<field0>', '<date>', '<field2>', '<field3>'])
# construct a new row for the output file, consisting of
# indices 0, the date, indices 2, 3, 4 and 1.
writer.writerow([row[0], file_date] + row[2:5] + [row[1]])
finally:
for fileobj in open_writing_files.itervalues():
fileobj.close()
以上:
csv.reader()
和csv.writer()
。csv
模块以这种方式正确处理换行处理。