gpt4 book ai didi

Python:使用自定义格式读取 CSV 并写入文件

转载 作者:行者123 更新时间:2023-11-30 22:39:58 25 4
gpt4 key购买 nike

我有这个 .csv 文件...

id,first_name,last_name,email,date,opt-in
1,Jimmy,Reyes,jreyes0@macromedia.com,12/29/2016,FALSE
2,Doris,Wood,dwood1@1und1.de,04/22/2016,
3,Steven,Miller,smiller2@go.com,07/31/2016,FALSE
4,Earl,Parker,eparker3@ucoz.com,01-08-17,FALSE
5,Barbara,Cruz,bcruz4@zdnet.com,12/30/2016,FALSE

我想读取上面显示的 csv 文件,转换数据,最后将数据写入另一个文本文件中,该文件应该如下所示......

1,<tab>"first_name"="Jimmy","last_name"="Reyes","email"="jreyes0@macromedia.com","date"="12/29/2016","opt-in"="FALSE"
2,<tab>"first_name"="Doris","last_name"="Wood","email"="dwood1@1und1.de","date"="04/22/2016,,"opt-in"="0"

此外,如果选择加入值为空,则应打印“0”。

这是到目前为止我的代码......

import csv
import time

# Do the reading
with open('my-scripts/mock.csv', 'r') as f1:
#next(f1, None) # skip the headers
reader = csv.reader(f1)
new_rows_list = []
for row in reader:
if row[5] == '':
new_row = [row[0],'\t',row[1], row[2], row[3], row[4], '0']
new_rows_list.append(new_row)
else:
new_row = [row[0],'\t',row[1], row[2], row[3], row[4], row[5]]
new_rows_list.append(new_row)
f1.close() # <---IMPORTANT

# Do the writing
newfilename = 'my-scripts/ftp_745198_'+str(int(time.time()))
with open(newfilename, 'w', newline='') as f2:
writer = csv.writer(f2, quoting=csv.QUOTE_NONNUMERIC)
writer.writerows(new_rows_list)
f2.close()

上面的代码正在生成此输出,这不是我真正想要的...我无法弄清楚如何在每行中打印列名称,如上面所需的输出所示...!

"id","  ","first_name","last_name","email","date","opt-in"
"1"," ","Jimmy","Reyes","jreyes0@macromedia.com","12/29/2016","FALSE"
"2"," ","Doris","Wood","dwood1@1und1.de","04/22/2016","0"
"3"," ","Steven","Miller","smiller2@go.com","07/31/2016","FALSE"
"4"," ","Earl","Parker","eparker3@ucoz.com","01-08-17","FALSE"
"5"," ","Barbara","Cruz","bcruz4@zdnet.com","12/30/2016","FALSE"

新 CSV

id,first_name,last_name,email,date,opt-in,unique_code
1,Jimmy,Reyes,jreyes0@macromedia.com,12/29/2016,FALSE,ER45DH
2,Doris,Wood,dwood1@1und1.de,04/22/2016,,MU34T3
3,Steven,Miller,smiller2@go.com,07/31/2016,FALSE,G34FGH
4,Earl,Parker,eparker3@ucoz.com,01-08-17,FALSE,ASY67J
5,Barbara,Cruz,bcruz4@zdnet.com,12/30/2016,FALSE,NHG67P

新的预期产出

ER45DH<tab>"id"="1","first_name"="Jimmy","last_name"="Reyes","email"="jreyes0@macromedia.com","date"="12/29/2016","opt-in"="FALSE"
MU34T3<tab>"id"="2","first_name"="Doris","last_name"="Wood","email"="dwood1@1und1.de","date"="04/22/2016,"opt-in"="0"

我将非常感谢任何帮助/想法/指示。

谢谢

最佳答案

  • 最初将 header 提取为新列表。

  • 然后将标题与每行元素作为字符串附加。

  • 将其写入文件。

请尝试此代码,

import csv

with open('newfilename.csv', 'w') as f2:
with open('mycsvfile.csv', mode='r') as infile:
reader = csv.reader(infile)
for i,rows in enumerate(reader):
if i == 0:
header = rows
else:
if rows[5] == '':
rows[5] = 0;
pat = rows[0]+'\t'+'''"%s=%%s",'''*(len(header)-1)+'\n'
print pat
f2.write(pat % tuple(header[1:]) % tuple(rows[1:]))
f2.close()

输出:

1   "first_name=Jimmy","last_name=Reyes","email=jreyes0@macromedia.com","date=12/29/2016","opt-in=FALSE",
2 "first_name=Doris","last_name=Wood","email=dwood1@1und1.de","date=04/22/2016","opt-in=0",
3 "first_name=Steven","last_name=Miller","email=smiller2@go.com","date=07/31/2016","opt-in=FALSE",
4 "first_name=Earl","last_name=Parker","email=eparker3@ucoz.com","date=01-08-17","opt-in=FALSE",
5 "first_name=Barbara","last_name=Cruz","email=bcruz4@zdnet.com","date=12/30/2016","opt-in=FALSE",

如有任何疑问,请告诉我。

关于Python:使用自定义格式读取 CSV 并写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42973642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com