繁体   English   中英

如何使用 python 从 .csv 文件中的行中提取数据到单独的 .txt 文件中?

[英]How to extract data from rows in .csv file into separate .txt files using python?

我有一个从 h5 文件导出的采访记录的 CSV 文件。 当我将行读入 python 时,输出如下所示:

    line[0]=['title,date,responses']
    line[1]=['[\'Transcript 1 title\'],"[\' July 7, 1997\']","[ '\nms. vogel: i look at all sectors of insurance, although to date i\nhaven\'t really focused on the reinsurers and the brokers.\n']'] 
    line[2]=['[\'Transcript 2 title\'],"[\' July 8, 1997\']","[ '\nmr. tozzi: i formed cambridge in 1981. we are top-down sector managers,\nconstantly searching for non-consensus companies and industries.\n']']
    etc...

我想将“响应”列中的文本仅提取到 CSV 文件中每一行的单独 .txt 文件中,将 .txt 文件保存到指定目录中并将它们命名为“t1.txt”、“t2.txt”。 txt”等根据行号。 CSV 文件大约有 30K 行。

根据我已经能够在网上找到的内容,这是我迄今为止的代码:

    import csv
    with open("twst.csv", "r") as f:
        reader = csv.reader(f)
        rownumber = 0
        for row in reader:
             g=open("t"+str(rownumber)+".txt","w")
             g.write(row)
             rownumber = rownumber + 1
             g.close()

我最大的问题是这会将行中的所有列拉入 .txt 文件,但我只想要“响应”列中的文本。 一旦我有了它,我知道我可以遍历文件中的各个行(现在,我设置的只是测试第一行),但我还没有找到任何关于在 python 中提取特定列的指导文档。 我对 python 也不够熟悉,无法自己找出代码。

在此先感谢您的帮助!

可能有一些事情可以用内置的 csv 模块来完成。 但是,如果 csv 的格式没有改变,下面的代码应该只使用 for 循环和内置读/写。

with open('test.csv', 'r') as file:
    data = file.read().split('\n')

for row in range(1, len(data)):
    third_col= data[x].split(',')
    with open('t' + str(x) + '.txt', 'w') as output:
        output.write(third_col[2])

暂无
暂无

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

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