我需要根据该列更改csv的值,
csv1:
Account User login status
guh guhan 1 upload
nav Naveen 2 complete
csv2:
Account User login status
guh guhan 1 complete
nav Naveen 2 complete
输出csv:
guh guhan 1 complete
nav Naveen 2 complete
由于第一行是csv的标题,因此需要从第二行进行检查。
while IFS="," read Account User login status
if [ $status in csv1 = $status in csv2 ]
dont change
if [$ status in csv1 != $staus in csv2 ]
change the status value
请帮助我
答案 0 :(得分:0)
如果您接受Python中的解决方案,那么您在这里:
将csv1.csv
赋予
Account,User,login,status
guh,guhan,1,upload
nav,Naveen,2,complete
和csv2.csv
为
Account,User,login,status
guh,guhan,1,complete
nav,Naveen,2,complete
您可以运行以下脚本output.py
:
#!/usr/bin/env python3
# Update status information from csv1 according to csv2
import csv
with open('csv1.csv') as cf:
reader = csv.DictReader(cf)
fieldnames = reader.fieldnames
rows1 = [ r for r in reader ]
with open('csv2.csv') as cf:
rows2 = [ r for r in csv.DictReader(cf) ]
KEY_FIELDS = ['Account','User','login']
FIELD_TO_UPDATE = 'status'
OVERWRITE_STATUS = 'complete'
with open('output.csv','w') as cf:
writer = csv.DictWriter(cf, fieldnames=fieldnames)
writer.writeheader()
for r1 in rows1:
r_new = r1
for r2 in rows2:
if all([ r1[f] == r2[f] for f in KEY_FIELDS ]):
if r2[FIELD_TO_UPDATE] == OVERWRITE_STATUS:
r_new[FIELD_TO_UPDATE] = OVERWRITE_STATUS
writer.writerow(r_new)
这将为您提供文件output.py
:
Account,User,login,status
guh,guhan,1,complete
nav,Naveen,2,complete
注意事项:此脚本非常很基本,并且无法缩放到大型文件csv1.csv
和csv2.csv
。