2017-03-24 155 views
-2

我有幾個包含這樣的數據文件:合併多個文本文件作爲一個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 
+2

你有沒有試圖寫任何東西呢?如果沒有,我相信下面的鏈接會給你你需要開始和一些搜索。 https://docs.python.org/3/library/csv.html – jsm1th

回答

1

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

0

我的方式:

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") 
0

你應該嘗試寫一些代碼自己:

我的方式:

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字符,並結合清單,寫他們成爲一個文件。