【发布时间】:2014-08-28 11:44:02
【问题描述】:
我有 3 个文件,其中包含真实数据和伪数据以及真实数据的值。
File_one 有两列,一列是真实数据,第二列是平移数据。 IE。对于真实数据,会给出一个伪值。
col[0] col[1]
123 0
234 1
345 2
456 3
567 4
678 5
File_two 具有成对的伪值,即代替 123,使用的值是 0,伪值对的方式与 [0, 1] 相同,这意味着实际的 [123, 234]。
col[0] col[1]
0 2
0 3
0 5
2 4
5 1
所以可以说file_two中的col[0] and col[1]是键,值在file_onecol[0]
现在我必须将file_two 中的伪值对与file_one 中的真实数据col[0] 进行匹配,并将输出保存到新文件中。我们将其命名为file_four。这里对只出现ONE时间。
col[0] col[1]
123 345
123 456
123 678
345 567
678 234
现在file_three 出现了。 File_three 有 3 列。
col[0] 和 col[1] 与 file_four 中的对相同,但它们还有许多其他对在 file_four 中不存在。
文件_三个
col[0] col[1] col[2]
123 345 54
345 262 65
123 456 54
2456 2467 98
123 678 46
7845 2458 631
345 567 153
3456 3673 94
678 234 5
最后,我需要匹配file_four 对,即col[0] col[1] 并从file_three 中的col[2] 中提取值,并生成一个新的output_file,其中file_four 对作为键和值在col[2] 的file_three 中。
在下面的代码中,我试图只考虑前两个文件
from collections import defaultdict
d1 = dict()
d2 = dict()
with open('input1.txt', 'r') as file1:
for row in file1:
c0, c1 = row.split()[:2]
d1[c1] = c0
with open('input2.txt', 'r') as file2:
for row in file2:
c0, c1 = row.split()[:2]
d2[(c0, c1)] = [d1[c1], d1[c1]]
#for k, v in sorted(d2.items()):
#print '\t'.join(v)
print d2
Error:
Key Error: 'key'
即使没有注释 for 循环并且注释了最后一个打印,也会出现相同的错误。
【问题讨论】:
-
这里发布错误时,最好发布完整的 Traceback。当您尝试从不存在的字典中检索某些内容时,会出现KeyError。当试图弄清楚这样的事情时,打印语句可以提供很大帮助。将语句包装在 Try/Except 块中并打印有问题的值,也许还有字典。在拆分之前,您可能需要从每行中去除空格。你可能想花一些时间在文档中的教程上,也许greenteapress.com/thinkpython
标签: python file dictionary multiple-columns