我有几个包含这样的数据文件:合并多个文本文件作为一个CSV文件
file1.txt
abc
def
hij
file2.txt
def
abc
qlm
file3.txt
def
lop
tmn
所需的输出:
mergedfile.csv
file1 file2 file2
abc def def
def abc lop
hij qlm tmn
我有几个包含这样的数据文件:合并多个文本文件作为一个CSV文件
file1.txt
abc
def
hij
file2.txt
def
abc
qlm
file3.txt
def
lop
tmn
所需的输出:
mergedfile.csv
file1 file2 file2
abc def def
def abc lop
hij qlm tmn
Python文档CSV File Reading and Writing
例如,最低存储器足迹:
import io, csv
fieldnames = [file1, file2, file3]
with io.open(csv_file, 'w', newline='') as fh_csv, \
open(file1) as fh1, \
open(file2) as fh2, \
open(file3) as fh3:
writer = csv.writer(fh_csv, delimiter='\t')
writer.writerow(fieldnames)
while True:
out = []
for fh in [fh1, fh2, fh3]:
out.append(fh.readline().strip('\n'))
if all(out):
writer.writerow(out)
else:
break
输出:
file1.txt file2.txt file3.txt
abc def def
def abc lop
hij qlm tmn
测试与Python:3.4.2
我的方式:
import pandas as pd
df1=pd.read_csv('file1.txt',names=['file1'])
df2=pd.read_csv('file2.txt',names=['file2'])
df3=pd.read_csv('file3.txt',names=['file3'])
result=pd.concat([df1,df2,df3],axis=1)
result.to_csv('mergedfile.txt',index=False)
or mor Ë蟒蛇风格:
import os
import pandas as pd
def merge_files(file_list, export_file):
df_list = [pd.read_csv(
one_file, names = [os.path.splitext(os.path.basename(one_file))[0]]
) for one_file in file_list if os.path.isfile(one_file)]
all_df=pd.concat(df_list,axis=1)
all_df.to_csv(export_file, index=False)
if __name__=='__main__':
sample_files=['./file1.txt', '../temp/file2.txt','file3.txt']
merge_files(sample_files,"mergedfile.txt")
你应该尝试写一些代码自己:
我的方式:
file_list=['a','b','c']
open("o","w").write("\n".join([" ".join(i).replace("\n","") for i in list(zip(*[list(open(i)) for i in file_list]))]))
结果:
file1.txt file2.txt file3.txt
abc def def
def abc lop
hij qlm tmn
我读三文件并将数据追加到三个列表,然后使用zip来聚集elements.If要与Python2.x
兼容,Python3.x
你可以将它添加到你的代码的顶部:
try:
from itertools import izip as zip
except ImportError:
pass
然后我删除了\n
字符,并结合清单,写他们成为一个文件。
你有没有试图写任何东西呢?如果没有,我相信下面的链接会给你你需要开始和一些搜索。 https://docs.python.org/3/library/csv.html – jsm1th