读取文件并将特定字段输出到CSV文件

时间:2019-03-26 20:37:01

标签: excel python-3.x output

我正在尝试根据关键字搜索数据,并将该数据导出到Excel或文本文件中。

当我“打印”变量/列表时,它没有问题。当我尝试将数据输出到文件时,它仅输出最后一个条目。我认为该迭代有问题,但我无法弄清楚。

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

我创建了此脚本,因此它将去查找“ config”文件中带有关键字“ ACL”的所有行。 然后,它可以打印之前的行,并找到数据的实际行。这很好。 我的下一步是将数据输出到Excel电子表格。这就是我卡住的地方。 该脚本仅打印A行10列中的最后一项。 我需要帮助弄清楚为什么它会正确打印数据,但不会将其输出到excel电子表格,甚至是.txt文件。

1 个答案:

答案 0 :(得分:1)

尝试一下-我将您的工作簿和工作表定义移到了循环之外,因此不会一直被重新定义。

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