简体   繁体   中英

Copy specific lines from multiple text files to an excel file

I have as many as 1500 text files and I want to copy 5 lines from every text file, say line 4,5,9,14 and 32. I want to make columns of these files in an excel sheet one below the other, of the 1500 text files. I have figured out a code that takes in only one txt file but copies all the data into rows. Any help will be appreciated. Here is my code:

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)

You would first need to put all of your required text files in the current folder, glob.glob('*.txt') could then be used to get a list of these filenames. For each text file, read the files in using readlines() and extract the required lines using itemgetter() . For each file, create a new row in your output worksheet and write each line as a different column entry.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM