你不是很清楚你正在使用的文件格式,或者你在读入记录后需要做什么。但是从your other question on reading CSV files获取一些文件格式,我会给你一个示例程序工作。
这是一个主文件m.csv.txt。这是一个逗号分隔值 (CSV) 文件,第一个字段是标识符。
AAC,D,20111207,9.83,9.83,9.83,9.83,100
AACC,D,20111207,3.46,3.47,3.4,3.4,13400
AACOW,D,20111207,0.3,0.3,0.3,0.3,500
AAME,D,20111207,1.99,1.99,1.95,1.99,8600
AAON,D,20111207,21.62,21.9,21.32,21.49,93200
AAPL,D,20111207,389.93,390.94,386.76,389.09,10892800
这是每日更新文件d.csv.txt。它具有相同的格式。同样,第一个字段是标识符。如果标识符与主文件中的标识符相同,则应合并条目。否则,每日文件中的新标识符将被添加到整个列表中。
AACC,D,20120127,4.01,4.02,4.03,4.04,40000
B,D,20120127,4.01,4.02,4.03,4.04,40000
这是一个使用csv module读入主文件的简单程序,并将每一行的条目放入字典stocks。第一个字段用作字典的键。这是一个非常简单的数据结构,但是您说主文件可以是任何数据格式。然后我们打印出stocks 字典。
import csv
mainReader = csv.DictReader( open("m.csv.txt","rb"), ["id"],"others")
newReader = csv.DictReader( open("d.csv.txt","rb"), ["id"],"others")
stocks = {}
for line in mainReader:
stocks[line['id']] = line
print stocks # output hand-formatted
{
'AAC': {'id':'AAC',
'others': ['D','20111207','9.83','9.83','9.83','9.83','100']},
'AACC': {'id':'AACC',
'others': ['D', '20111207', '3.46', '3.47', '3.4', '3.4', '13400']},
'AAME': {'id': 'AAME',
'others': ['D', '20111207', '1.99', '1.99', '1.95', '1.99', '8600']},
'AACOW': {'id': 'AACOW',
'others': ['D', '20111207', '0.3', '0.3', '0.3', '0.3', '500']},
'AAPL': {'id': 'AAPL', 'others':
['D','20111207','389.93','390.94','386.76','389.09','10892800']},
'AAON': {'id': 'AAON',
'others': ['D', '20111207', '21.62', '21.9', '21.32', '21.49', '93200']}
}
现在程序继续读取每日更新文件,再次使用 csv 模块。如果条目具有相同的标识符,您不清楚应如何将条目与主文件中的条目组合。对于此示例,我将让每日文件中的条目完全替换现有条目。我们打印出结果stocks。
for line in newReader:
# can use if line['id'] in stocks: to check for an existing record if desired
# in this example, we blindly overwrite any existing entry
stocks[line['id']] = line
print stocks # output hand-formatted
{
'AAME': {'id': 'AAME',
'others': ['D', '20111207', '1.99', '1.99', '1.95', '1.99', '8600']},
'B': {'id': 'B',
'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']},
'AACC': {'id': 'AACC',
'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']},
'AAPL': {'id': 'AAPL',
'others': ['D','20111207','389.93','390.94','386.76','389.09','10892800']},
'AAON': {'id': 'AAON',
'others': ['D', '20111207', '21.62', '21.9', '21.32', '21.49', '93200']},
'AACOW': {'id': 'AACOW',
'others': ['D', '20111207', '0.3', '0.3', '0.3', '0.3', '500']}
}
请注意,字典数据结构在打印时没有任何特定顺序。你想要排序输出?对字典使用items() 方法获取列表,使用.sort() 对列表进行排序。 (还有很多其他方法可以做到这一点。)
s = stocks.items()
s.sort()
print s # output hand-formatted
[
'AACC': {'id': 'AACC',
'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']},
'AACOW': {'id': 'AACOW',
'others': ['D', '20111207', '0.3', '0.3', '0.3', '0.3', '500']},
'AAME': {'id': 'AAME',
'others': ['D', '20111207', '1.99', '1.99', '1.95', '1.99', '8600']},
'AAON': {'id': 'AAON',
'others': ['D', '20111207', '21.62', '21.9', '21.32', '21.49', '93200']},
'AAPL': {'id': 'AAPL',
'others': ['D','20111207','389.93','390.94','386.76','389.09','10892800']},
'B': {'id': 'B',
'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']}
]
你想写出一个更新的主文件。使用csv.DictWriter() 或csv.writer()。您可以逐行执行此操作,也可以一次输出完整的内存列表,如stocks。