2017-07-28 113 views
0

我有多達1500文本文件,我想5線從每一個文本文件複製,說線4,5,9,14和32我想打這些文件的列在1500個文本文件的Excel工作表中一個在另一個之下。我已經想出了只需要一個txt文件但將所有數據複製到行中的代碼。任何幫助將不勝感激。 這裏是我的代碼:複製多個文本文件的特定行到Excel文件

import csv 
import xlwt 

import os 
import sys 

# Look for input file in same location as script file: 
inputfilename = os.path.join(os.path.dirname(sys.argv[0]), 
'C:/path/filename.txt') 
# Strip off the path 
basefilename = os.path.basename(inputfilename) 
# Strip off the extension 
basefilename_noext = os.path.splitext(basefilename)[0] 
# Get the path of the input file as the target output path 
targetoutputpath = os.path.dirname(inputfilename) 
# Generate the output filename 
outputfilename = os.path.join(targetoutputpath, basefilename_noext + '.xls') 

# Create a workbook object 
workbook = xlwt.Workbook() 
# Add a sheet object 
worksheet = workbook.add_sheet(basefilename_noext, cell_overwrite_ok=True) 

# Get a CSV reader object set up for reading the input file with tab 
delimiters 
datareader = csv.reader(open(inputfilename, 'rb'), 
        delimiter='\t', quotechar='"') 

# Process the file and output to Excel sheet 

for rowno, row in enumerate(datareader): 
    for colno, colitem in enumerate(row): 

    worksheet.write(rowno, colno, colitem) 

# Write the output file. 
workbook.save(outputfilename) 

# Open it via the operating system (will only work on Windows) 
# On Linux/Unix you would use subprocess.Popen(['xdg-open', filename]) 
os.startfile(outputfilename) 
+0

因此,你想有一個你感興趣的每一行的列和你從這些行提取的每一個文件的行?你必須使用'xlwt'嗎? – albert

回答

0

你首先就需要把所有需要的文本文件在當前文件夾,glob.glob('*.txt')然後可以用來獲取這些文件名列表。對於每個文本文件,使用readlines()讀取文件並使用itemgetter()提取所需的行。對於每個文件,請在輸出工作表中創建一個新行,並將每行寫入不同的列條目。

import xlwt 
import glob 
import operator 

# Create a workbook object 
wb = xlwt.Workbook() 
# # Add a sheet object 
ws = wb.add_sheet('Sheet1', cell_overwrite_ok=True) 
rowy = 0 

for text_filename in glob.glob('*.txt'): 
    with open(text_filename) as f_input: 
     try: 
      lines = [line.strip() for line in operator.itemgetter(4, 5, 9, 14, 32)(f_input.readlines())] 
     except IndexError as e: 
      print "'{}' is too short".format(text_filename) 
      lines = [] 

    # Output to Excel sheet 
    for colno, colitem in enumerate(lines): 
     ws.write(rowy, colno, colitem) 

    rowy += 1 

# Write the output file. 
wb.save('output.xls') 
相關問題