这听起来像是我在关系型(即SQL)数据库中要做的事情,那里有很多工具可用于验证您的数据并确保所有数据保持一致
如果要在Python中执行此操作,则可以执行以下操作:
# put your "lists of prices" into a dictionary,keyed by the reference number
# assuming the prices is in the form [(ref1,price1),(ref2,price2)]
ref_prices = {}
for ref,price in PRICE_list:
ref_prices[ref] = price
# do the same for each additional list:
# shorter syntax than the above
ref_quantity = {ref: qty for ref,qty in QTY_list}
# combine all of the above and write into a file
with open(filename,'w') as fd:
out = csv.writer(fd,delimiter=';')
for id,ref in ID_list:
out.writerow((id,ref,ref_prices[ref],ref_quantity[ref]))
,
这是SQL的完美用例。如果要在Python中实现类似SQL的函数,通常最好使用pandas。它很方便,易于读写,而且速度很快。对于您的情况,假设其他值将存储在元组列表或字典中:
import pandas as pd
csv = [
(1,10),(2,20),(3,30),]
csv_df = pd.DataFrame(csv,columns=["id","reference"])
# This would be the data you have in your csv. For actually loading them from your
# csv located at `filepath`,use
#
# pd.DataFrame.read_csv(filepath)
additional_data = [
(1,"a"),"b"),"c"),] # This could also be a dictionary
additional_df = pd.DataFrame(additional_data,"name"])
final_df = csv_df.merge(additional_df,on="id")
然后我们得到
>>> final_df
id reference name
0 1 10 a
1 2 20 b
2 3 30 c
本文链接:https://www.f2er.com/3125591.html