在从csv中提取数据并写入文本文件时需要帮助

时间:2019-09-05 16:10:10

标签: python csv

我有一个包含两列数据的csv。我想从一列中提取数据,然后将其写入文本文件,并在每个元素上用单引号引起来,并用逗号分隔。例如,我有这个..

taxable_entity_id,id
45efc167-9254-406c-b5a8-6aef91a73dd9,331999
5ae97680-f489-4182-9dcb-eb07a73fab15,103507
00018d93-ae71-4367-a0da-f252cea4dfa2,32991

我想要这样的文本文件中的所有taxable_entity_ids

'45efc167-9254-406c-b5a8-6aef91a73dd9','5ae97680-f489-4182-9dcb-eb07a73fab15','00018d93-ae71-4367-a0da-f252cea4dfa2'

两个元素之间没有空格,以逗号分隔。

编辑:

这是我尝试过的。

import csv


with open("Taxable_entity_those_who_filed_G1_M_July_but_not_in_Aug.csv", 'r') as csv_File:

    reader = csv.DictReader(csv_File)

    with open("te_id.csv", 'w') as text_file:
        writer = csv.writer(text_file, quotechar='\'', quoting=csv.QUOTE_MINIMAL)

        for row in reader:
            writer.writerow(row["taxable_entity_id"])
            # print(row["taxable_entity_id"])

    text_file.close()

csv_File.close()

这就是我得到的。

4,5,e,f,c,1,6,7,-,9,2,5,4,-,4,0,6,c,-,b,5,a,8,-,6,a,e,f,9,1,a,7,3,d,d,9
5,a,e,9,7,6,8,0,-,f,4,8,9,-,4,1,8,2,-,9,d,c,b,-,e,b,0,7,a,7,3,f,a,b,1,5
0,0,0,1,8,d,9,3,-,a,e,7,1,-,4,3,6,7,-,a,0,d,a,-,f,2,5,2,c,e,a,4,d,f,a,2

3 个答案:

答案 0 :(得分:2)

你很近。只需在输出文件中只显示一行,就应该使用一个理解力一次编写它:

import csv


with open("Taxable_entity_those_who_filed_G1_M_July_but_not_in_Aug.csv", 'r') as csv_File:

    reader = csv.DictReader(csv_File)

    with open("te_id.csv", 'w') as text_file:
        # use QUOTE_ALL to force the quoting
        writer = csv.writer(text_file, quotechar='\'', quoting=csv.QUOTE_ALL)

        writer.writerow((row["taxable_entity_id"] for row in reader))

并且不要像正确使用close那样使用with

答案 1 :(得分:0)

尝试

import pandas as pd
df = pd.read_csv('nameoffile.csv',delimiter = ',')
X = df[0].values
f = open('newfile.txt','w')
for i in X:
    f.write(X[i] + ',')
f.close()

答案 2 :(得分:0)

您基本上想要taxable_entity_ids的一行csv文件似乎有些奇怪,但是确实有可能。您也不需要显式close()打开的文件,因为with上下文管理器将自动为您完成此操作。

您还需要使用newline=''打开CSV文件,如csv模块的documentation中的所有示例所示。

最后,如果要引用所有字段,则需要使用quoting=csv.QUOTE_ALL而不是quoting=csv.QUOTE_MINIMAL

import csv


inp_filename = "Taxable_entity_those_who_filed_G1_M_July_but_not_in_Aug.csv"
outp_filename = "te_id.csv"

with open(outp_filename, 'w', newline='') as text_file, \
     open(inp_filename, 'r', newline='') as csv_File:

    reader = csv.DictReader(csv_File)
    writer = csv.writer(text_file, quotechar="'", quoting=csv.QUOTE_ALL)

    taxable_entity_ids = (row["taxable_entity_id"] for row in reader)
    writer.writerow(taxable_entity_ids)

print('done')