简体   繁体   English

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

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

I have a CSV file of interview transcripts exported from an h5 file.我有一个从 h5 文件导出的采访记录的 CSV 文件。 When I read the rows into python, the output looks something like this:当我将行读入 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...

I'd like to extract the text from the "responses" column ONLY into separate .txt files for every row in the CSV file, saving the .txt files into a specified directory and naming them as "t1.txt", "t2.txt", etc. according to the row number.我想将“响应”列中的文本仅提取到 CSV 文件中每一行的单独 .txt 文件中,将 .txt 文件保存到指定目录中并将它们命名为“t1.txt”、“t2.txt”。 txt”等根据行号。 The CSV file has roughly 30K rows. CSV 文件大约有 30K 行。

Drawing from what I've already been able to find online, this is the code I have so far:根据我已经能够在网上找到的内容,这是我迄今为止的代码:

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

My biggest problem is that this pulls all columns from the row into the .txt file, but I only want the text from the "responses" column.我最大的问题是这会将行中的所有列拉入 .txt 文件,但我只想要“响应”列中的文本。 Once I have that, I know I can loop through the various rows in the file (right now, what I have set up is just to test the first row), but I haven't found any guidance on pulling specific columns in the python documentation.一旦我有了它,我知道我可以遍历文件中的各个行(现在,我设置的只是测试第一行),但我还没有找到任何关于在 python 中提取特定列的指导文档。 I'm also not familiar enough with python to figure out the code on my own.我对 python 也不够熟悉,无法自己找出代码。

Thanks in advance for the help!在此先感谢您的帮助!

There may be something that can be done with the built-in csv module.可能有一些事情可以用内置的 csv 模块来完成。 However, if the format of the csv does not change, the following code should work by just using for loops and built-in read/write.但是,如果 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