Home » excel » excel – Read file and output specific fields to CSV file

excel – Read file and output specific fields to CSV file

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to search for data based on a key word and export that data to an Excel or text file.

When I “print” the variable/list it works no problem. When I try and output the data to a file it only outputs the last entry. I think something is wrong with the iteration, but I can’t figure it out.

import xlsxwriter

#Paths
xls_output_path = 'C:\Data\'   
config = 'C:\Configs\filename.txt'

excel_inc = 0  #used to increment the excel columns so not everything 
               #is written in "A1"

lines = open(config,"r").read().splitlines()

search_term = "ACL"



for i, line in enumerate(lines):
    if search_term in line:       
        split_lines = line.split(' ')   #Split lines via a space.
        linebefore = lines[i - 1]      #Print the line before the search term
        linebefore_split = linebefore.split(' ') #Split the line before via 
                                                 #space
        from_obj = linebefore_split[2]   #[2] holds the data I need
        to_object = split_lines[4]       #[4] holds the data I need
        print(len(split_lines))       #Prints each found line with no 
                                       #problem.

        excel_inc = excel_inc + 1      #Increments for column A so not all of 
                                       #the data is placed in A1
        excel_inc_str = str(excel_inc) #Change type to string so it can 
                                       #concatenate.


        workbook = xlsxwriter.Workbook(xls_output_path + 'Test.xlsx') #Creates the xls file
        worksheet = workbook.add_worksheet()

        worksheet.write('A' + excel_inc_str, split_lines[4]) #Write data from 
                                                             #split_lines[4] 
                                                             #to column A
workbook.close()

I created this script so it will go and find all lines in the “config” file with the keyword “ACL”.
It then has the ability to print the line before and the actual line the data is found. This works great.
My next step is outputting the data to an excel spreadsheet. This is where I get stuck.
The script only prints the very last item in the column A row 10.
I need help figuring out why it’ll print the data correctly, but it won’t output it to an excel spreadsheet or even a .txt file.

How to&Answers:

Try this – I moved your workbook and worksheet definitions outside the loop, so it doesn’t keep getting redefined.

import xlsxwriter

#Paths
xls_output_path = 'C:\Data\'   
config = 'C:\Configs\filename.txt'

excel_inc = 0  #used to increment the excel columns so not everything 
               #is written in "A1"

lines = open(config,"r").read().splitlines()

search_term = "ACL"

workbook = xlsxwriter.Workbook(xls_output_path + 'Test.xlsx') #Creates the xls file
worksheet = workbook.add_worksheet()

for i, line in enumerate(lines):
    if search_term in line:       
        split_lines = line.split(' ')   #Split lines via a space.
        linebefore = lines[i - 1]      #Print the line before the search term
        linebefore_split = linebefore.split(' ') #Split the line before via 
                                                 #space
        from_obj = linebefore_split[2]   #[2] holds the data I need
        to_object = split_lines[4]       #[4] holds the data I need
        print(len(split_lines))       #Prints each found line with no 
                                       #problem.

        excel_inc = excel_inc + 1      #Increments for column A so not all of 
                                       #the data is placed in A1
        excel_inc_str = str(excel_inc) #Change type to string so it can 
                                       #concatenate.




        worksheet.write('A' + excel_inc_str, split_lines[4]) #Write data from 
                                                             #split_lines[4] 
                                                             #to column A
workbook.close()