【发布时间】:2021-10-19 01:25:25
【问题描述】:
我有 2 个 csv 文件,
- 文件
zulu包含基本信息,分列。 - 文件
bommel仅更新了相同列中具有相同记录的信息。
我想在没有 Pandas 或其他外部资源的情况下在 Python 中解决这个问题(使用标准库中的 csv 模块)。
#!/usr/bin/env python3
import csv
# Define column names
fields = ['capcode', 'discipline', 'region', 'location', 'description', 'remark']
# Open the neccesary files
with open('bommel_db_capcodes.txt', 'r') as readFile_bommel:
with open('results.csv', 'w') as results:
with open('zulu_db_capcodes.txt', 'r') as readFile_zulu:
master = csv.DictReader(readFile_zulu, fieldnames=fields)
update = csv.DictReader(readFile_bommel, fieldnames=fields)
writer = csv.DictWriter(results, fieldnames=fields)
# Saves and skips header to output file
writer.writerow(next(master))
# Goes through whole zulu csv
for row in master:
for row2 in update:
if row['capcode'] in update:
writer.writerow(row2)
else:
writer.writerow(row)
ReadFilezulu.close()
ReadFilebommel.close()
results.close()
zulucsv的内容:
capcode,discipline,region,location,description,remark
000400001,Brandweer,Groningen,Groningen,Regionaal,Pelotonscommandant Logistiek/Water (Noord)
000400002,Brandweer,Groningen,Groningen,,
000400003,Brandweer,Groningen,Groningen,Regionaal,Pelotonscommandant Logistiek/Water) (Oost)
000100000,Brandweer,Amsterdam-Amstelland,Amsterdam-Amstelland,Aalsmeer,Postalarm
000100001,Brandweer,Amsterdam-Amstelland,Amsterdam-Amstelland,,
000100002,Brandweer,Amsterdam-Amstelland,Amsterdam-Amstelland,,Banaanzulu
000100003,Brandweer,Amsterdam-Amstelland,Amsterdam-Amstelland,,
bommelcsv的内容:
capcode,discipline,region,location,description,remark
000100000,Brandweer,Amsterdam-Amstelland,,banaanProefalarm,
000100001,Brandweer,Amsterdam-Amstelland,Aalsmeer,Bevelvoerders,
000100004,Brandweer,Amsterdam-Amstelland,Aalsmeer,Korpsalarm,
当前结果
capcode,discipline,region,location,description,remark
000400001,Brandweer,Groningen,Groningen,Regionaal,Pelotonscommandant Logistiek/Water (Noord)
000400001,Brandweer,Groningen,Groningen,Regionaal,Pelotonscommandant Logistiek/Water (Noord)
000400001,Brandweer,Groningen,Groningen,Regionaal,Pelotonscommandant Logistiek/Water (Noord)
预期结果
capcode,discipline,region,location,description,remark < from saving header
000400001,Brandweer,Groningen,Groningen,Regionaal,Pelotonscommandant Logistiek/Water (Noord) < from zulu
000400002,Brandweer,Groningen,Groningen,, < from zulu
000400003,Brandweer,Groningen,Groningen,Regionaal,Pelotonscommandant Logistiek/Water) (Oost) < from zulu
000100000,Brandweer,Amsterdam-Amstelland,,banaanProefalarm, < from bommel
000100001,Brandweer,Amsterdam-Amstelland,Aalsmeer,Bevelvoerders, < from bommel
000100002,Brandweer,Amsterdam-Amstelland,Amsterdam-Amstelland,,Banaanzulu < from zulu
000100003,Brandweer,Amsterdam-Amstelland,Amsterdam-Amstelland,, < from zulu
000100004,Brandweer,Amsterdam-Amstelland,Aalsmeer,Korpsalarm, < from bommel
关于如何完成这项工作的任何想法?
【问题讨论】:
-
您需要以特定方式对行进行排序吗?如果有,怎么做?
-
不,排序不是必需的,(可以很方便,在字段capcode上)
标签: python python-3.x csv export-to-csv