您好我正在尝试根据公共列或主键合并两个 CSV 文件中的特定字段来创建一个新的 CSV 文件。我曾尝试在 powershell 中做同样的事情,它工作但完成过程非常慢 - 合并 5000+ 行文件超过 30 分钟,所以在 Python 中尝试这个。我是新人,所以请放轻松。
所以两个文件是 infile.csv 和 checkfile.csv,并且创建的输出文件中的列将基于 infile.csv 中的列。代码检查 checkfile.csv 中的值,创建 outfile.csv,从 infile.csv 复制列,并需要根据 checkfile.com 中的相应值重写两个字段的值。以下是详细内容
infile.csv -
"StockNumber","SKU","ChannelProfileID","CostPrice"
"10m_s-vid#APTIIAMZ","2VV-10",3746,0.33
"10m_s-vid#CSE","2VV-10",3746,0.98
"1RR-01#CSE","1RR-01",3746
"1RR-01#PCAWS","1RR-01",3746,
"1m_s-vid_ext#APTIIAMZ","2VV-101",3746,0.42
检查文件.csv
ProductCode, Description, Supplier, CostPrice, RRPPrice, Stock, Manufacturer, SupplierProductCode, ManuCode, LeadTime
2VV-03,3MTR BLACK SVHS M - M GOLD CABLE - B/Q 100,Cables Direct Ltd,0.43,,930,CDL,2VV-03,2VV-03,1
2VV-05,5MTR BLACK SVHS M - M GOLD CABLE - B/Q 100,Cables Direct Ltd,0.54,,1935,CDL,2VV-05,2VV-05,1
2VV-10,10MTR BLACK SVHS M - M GOLD CABLE - B/Q 50,Cables Direct Ltd,0.86,,1991,CDL,2VV-10,2VV-10,1
我得到的 outfile.csv 是 -
StockNumber,SKU,ChannelProfileID,CostPrice
10m_s-vid#APTIIAMZ,2VV-10,"(' ',)",
10m_s-vid#CSE,2VV-10,"(' ',)",
1RR-01#CSE,1RR-01,"(' ',)",
1RR-01#PCAWS,1RR-01,"(' ',)",
1m_s-vid_ext#APTIIAMZ,2VV-101,"(' ',)",
但我需要的 outfile.csv 是 -
StockNumber,SKU,ChannelProfileID,CostPrice
10m_s-vid#APTIIAMZ,2VV-10,1991,0.86
10m_s-vid#CSE,2VV-10,1991,0.86
1RR-01#CSE,1RR-01
1RR-01#PCAWS,1RR-01
1m_s-vid_ext#APTIIAMZ,2VV-101
最后是代码 -
import csv
with open('checkfile.csv', 'rb') as checkfile:
checkreader = csv.DictReader(checkfile)
product_result = dict(
((v['ProductCode'], v[' Stock']), (v['ProductCode'], v[' CostPrice'])) for v in checkreader
)
with open('infile.csv', 'rb') as infile:
with open('outfile.csv', 'wb') as outfile:
reader = csv.DictReader(infile)
writer = csv.DictWriter(outfile, reader.fieldnames)
writer.writeheader()
for item in reader:
result = product_result.get(item['SKU'], " ")
item['ChannelProfileID'] = result,
item['CostPrice'] = result
writer.writerow(item)