Python 从csv读取项目并在另一个csv中更新相同的项目
我正在研究一种从Python 从csv读取项目并在另一个csv中更新相同的项目,python,list,csv,ordereddict,Python,List,Csv,Ordereddict,我正在研究一种从input.csv读取数据的方法,并根据产品的id更新output.csv中的stock列 以下是我目前正在进行的步骤: 1.将产品信息从input.csv读取到input\u data=[]中,这将返回订购信息列表 input_data当前看起来如下所示: [OrderedDict([('id','1'),('name','a'),('stock','33')), OrderedDict([('id','2'),('name','b'),('stock','66')),Orde
input.csv
读取数据的方法,并根据产品的id
更新output.csv
中的stock
列
以下是我目前正在进行的步骤:
1.将产品信息从input.csv
读取到input\u data=[]
中,这将返回订购信息列表
input_data
当前看起来如下所示:
[OrderedDict([('id','1'),('name','a'),('stock','33')),
OrderedDict([('id','2'),('name','b'),('stock','66')),OrderedDict([('id','3'),('name','c'),('stock','99'))]
2.将当前产品信息从output.csv
读取到output\u data=[]
,该数据与input\u data具有相同的模式
3.迭代input\u data
并根据input\u data
中的股票信息更新output\u data
中的stock
列最好的方法是什么?
->重要的一点是,在input\u data
中,可能有一些ID存在于input\u data
中,但不存在于output\u data
中。我想更新输入数据和输出数据共有的id
股票,“新的”id
很可能会写入新的csv
我在想这样的事情(这不是真正的代码):
我知道这看起来很混乱,我要求的是一种合乎逻辑的方式来完成这项任务,而不浪费太多的计算时间。所讨论的文件可能有100000行长,因此性能和速度将是一个问题
如果来自input_data
和output_data
的我的数据是OrderedDict
的列表
,在input\u data
中检查id
并在output\u data
中使用完全相同的id
将stock
写入产品的最佳方法是什么?虽然Python可能不是您的最佳选择,但我不会使用OrderDict列表来完成此任务。这仅仅是因为试图在output\u data
中更改某些内容将需要O(n)复杂度,这将简单地在O(n**2)中转换脚本。
我会将这两个文件保存在dicts(或者OrderedDicts,如果您关心顺序的话)中,就像这样(并将整个事情的复杂性降低到O(n)):
我建议您为这项任务检查pandas:谢谢,这让我朝着正确的方向前进了一步,这就是我希望使用的那种数据结构。但是,我似乎无法以这种格式从csv读取数据,因此每一行都是'1':['a','33']
。请给出一些如何实现这一点的提示?我想你应该逐行阅读,它类似于data=line.split(分隔符),然后输入_data[data[0]]=data[1:],谢谢!Mulțumesc:)
for p in input_data:
# check if p['id'] exists in the list of output_data IDs (I might have to create a list of IDs in output_data for this as well, in order to check it against input_data IDs
# if p['id'] exists in output_data, write the Stock to the corresponding product in output_data
# else, append p to another_csv
input_data = {
'1': ['a', '33'],
'2': ['b', '66'],
'3': ['c', '99']
}
output_data = {
'1': ['a', '31'],
'3': ['c', '95']
}
# iterate through all keys in input_data and update output_data
# if a key does not exist in output_data, create it in a different dict
new_data = {}
for key in input_data:
if key not in output_data:
new_data[key] = input_data[key]
# for optimisation's sake you could append data into the new file here
# and not save into a new dict
else:
output_data[key][1] = input_data[key][1]
# for optimisation's sake you could append data into a new output file here
# and rename/move the new output file into the old output file after the script finishes