如何使用python将.csv文件中的行数据提取到单独的.txt文件中? [英] How to extract data from rows in .csv file into separate .txt files using python?

查看:384
本文介绍了如何使用python将.csv文件中的行数据提取到单独的.txt文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从h5文件导出的采访记录的CSV文件.当我将行读入python时,输出看起来像这样:

I have a CSV file of interview transcripts exported from an h5 file. When I read the rows into python, the output looks something like this:

    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"等. CSV文件大约有3万行.

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. The CSV file has roughly 30K rows.

从我已经可以在网上找到的内容中得出,这是我到目前为止的代码:

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

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

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. 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. I'm also not familiar enough with python to figure out the code on my own.

提前感谢您的帮助!

推荐答案

内置的csv模块可以完成某些操作.但是,如果csv的格式没有更改,则下面的代码应该仅通过使用for循环和内置的读/写功能即可工作.

There may be something that can be done with the built-in csv module. 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.

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

这篇关于如何使用python将.csv文件中的行数据提取到单独的.txt文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆