我刚开始学习Python,我需要一个实习时让我写的脚本的帮助。在
我有一个csv文件(sheet1.csv),我只需要从标题referenceID和PartNumber相互对应的两个列中提取数据。我需要更新一个名为sheet2.csv的单独csv文件,该文件还包含referenceID和PartNumber两列,但是许多PartNumber单元格都是空的。在
基本上,我需要用sheet1中的值填充“PartNumber”字段。根据我所做的研究,我决定使用字典是写这个脚本的一个可靠的方法(我认为)。到目前为止,我已经能够阅读这些文件并创建两个字典,其中referenceid作为键,PartNumber作为值……下面是我展示的字典的示例。在
import csv
a = open('sheet1.csv', 'rU')
b = open('sheet2.csv', 'rU')
csvReadera = csv.DictReader(a)
csvReaderb = csv.DictReader(b)
a_dict = {}
b_dict = {}
for line in csvReadera:
a_dict[line["ReferenceID"]] = line["PartNumber"]
print(a_dict)
for line in csvReaderb:
b_dict[line["ReferenceID"]] = line["PartNumber"]
print(b_dict)
a_dict = {'R150': 'PN000123', 'R331': 'PN000873', 'C774': 'PN000064', 'L7896': 'PN000447', 'R0640': 'PN000878', 'R454': 'PN000333'}
b_dict = {'C774': '', 'R331': '', 'R454': '', 'L7896': 'PN000000', 'R0640': '', 'R150': 'PN000333'}
如何比较这两个字典,并填写/覆盖b-dict的缺失值,然后写入表2?当然,肯定有比我所想到的更有效的方法,但是我以前从未使用过Python,所以请原谅我可怜的尝试!在
看看熊猫图书馆。在
让我们来看看你定义为testdata的dicts
^{pr2}$你明白了吗
现在
这就是你想要的
刚准备好
并转储到csv
相关问题 更多 >
编程相关推荐