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

日本87

我正在尝试根据关键字搜索数据,并将该数据导出到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()

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

Kai Aeberli

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何过滤具有特定标签值的 json 文件并将输出输出到 csv

输出到文件并读取

Java读取文件并将其输出到控制台

如何比较两个 csv 文件中的特定列并将差异输出到第三个文件

psql-读取SQL文件并输出到CSV

尝试从CSV文件的特定字段读取

出口输出到CSV文件?

Perl输出到CSV文件

从特定的CSV列计算并输出到新文件

如何读取多个 .nc 文件并将其导出到不同的 .csv 文件?

比较2个不同的csv文件,并将所有更改输出到新的csv中

如何从输入文件读取并保存每行的某些部分并将其输出到命令行?

Powershell-读取列表并将gwmi内容输出到单独的文件

从文件读取并输出到套接字

Ansible 读取 csv 文件并加密密码,将命令输出到文件

此Python代码的优化-网络抓取并将结果输出到CSV文件

我可以grep多个文件中的文本并将结果输出到单个.csv吗?

遍历目录并将文件名输出到txt文件

连接特定行并将结果输出到新文件中

循环浏览CSV文件并将计算结果输出到一个统一的CSV文件

将groupby输出到csv文件熊猫

python:将列表输出到csv文件

Python Scrapy不输出到csv文件

xgettext输出到特定文件夹

将电子邮件数据导出到CSV文件(或XLS文件)中的特定字段

如何读取多个XML文件,然后输出到具有相同XML文件名的多个CSV文件

处理awk中的时间并将输出输出到csv文件中的新列的最佳方法是什么?

如何从2行文本文件读取并将每行输出到vb.net中的2个变量

计算 CSV 文件中的行数并将结果导出到 CSV