简体   繁体   English

将部分txt文件提取到excel的脚本

[英]Script to extract part of txt file to an excel

I have a txt file that looks like this:我有一个看起来像这样的 txt 文件:

category test_1

    aaa.com; test info - tw
    bbb.com; test info - al

category “test_2”

    ccc.com; test info - al
    ddd.com; test info - tw
    eee.com; test info - tw

category test_3

    fff.com
    ggg.com; test info - al
    hhh.com; test info - tw
    iii.com; test info - al

I need help editing a Python script that pulls a portion of the txt file and exports it to an excel file.我需要帮助编辑一个 Python 脚本,该脚本提取 txt 文件的一部分并将其导出到 excel 文件。 For example, if I want to export the entries in category test_1, the script would produce the following output in an excel file.例如,如果我想导出 test_1 类别中的条目,脚本将在 excel 文件中生成以下输出。

A一个 B C C
1 1 aaa.com aaa.com test info - tw测试信息 - tw
2 2 bbb.com bbb.com test info - al测试信息 - al
3 3

I have tried to use the code below我尝试使用下面的代码

My txt file is saved on my desktop as autotest.txt我的 txt 文件作为 autotest.txt 保存在我的桌面上


file=open(“autotest.txt”,’r’)
data=file.read()
categories=data.split(‘category’)
dict_format={}
for categor_data in categories:
    items=categor_data.split(‘\n’)
    dict_format[items[0].replace(“ “, “”)=items[1:]

for name in dict_format:
    print(name)

print(“Which category to export to .csv?”)
answer=input()

with open(answer+”.csv”,’w’) as csv:
    for row in dict_format[answer][:-1]:
        if row != “”:
            csv.write(row.replace(“;”,”,”)+”\n”)

    csv.write(dict_format[answer][-1].replace(“;”,”,”))
    csv.close()

When I run this code, it works correctly and returns an excel file for test_1 and test_3 but “test_2” does not return a file.当我运行此代码时,它可以正常工作并为 test_1 和 test_3 返回一个 excel 文件,但“test_2”不返回文件。 I am not sure why this occurs as I am accounting for and entering “test_2” in the question including the quotes.我不确定为什么会发生这种情况,因为我正在考虑并在包括引号的问题中输入“test_2”。 I have also tried entering it in the question without and get a file but it doesn't contain the entries.我也试过在没有的问题中输入它并获取一个文件,但它不包含条目。

Any help as to why the quotes is causing this error is much appreciated.非常感谢有关引号为何导致此错误的任何帮助。

Thanks!谢谢!

When printing the keys:打印密钥时:

print(dict_format.keys())

Return:返回:

dict_keys([“, ‘test_1’, ‘“test_2”’, ‘test_3’])

Changing:改变:

dict_format[items[0].replace(“ “, “”)]=items[1:]

To this:对此:

item_name=items[0].replace(“ “, “”)
item_name=item_name.strip(‘“‘)
dict_format[item_name]=items[1:]

This changes how the program reads the category and produces the expected output.这会改变程序读取类别并产生预期输出的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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