如何比较字典中的零填充数字和非零填充数字

时间:2014-02-03 14:59:54

标签: python python-2.6

我有两个文件,我需要比较它们和两个文件。从第二个文件更新第一个文件的值。

我的第一个文件如下,

SeqNo   City           State
1   Chicago         IL
2   Boston          MA
3   New York    NY
4   Los Angeles CA
5   Seattle         WA

我的第二个文件如下,

SeqNo   City           State   NewSeqNo
005   Seattle     WA  001
001   Chicago         IL  002
004   Los Angeles CA  003
002   Boston          MA  004
003   New York    NY  005

我有以下代码用第二个文件&中的NewSeqNo中的值更新第一个文件中的SEQ号。将其另存为第三个文件。但它会引发关键错误,因为SEQNO在第二个文件中填零,而不在第一个文件中,

import csv

lookup = {}
with open('secondfile') as f:
    reader = csv.reader(f)
    for line in reader:
        oldseq, city, state, newseq = line
        lookup[oldseq] = newseq


with open('firstfile') as f, open('outfile','w') as w:
    reader = csv.reader(f)
    writer = csv.writer(w)
    for line in reader:
        seq, city, state = line
        if seq in lookup:
            seq = lookup[seq]
        writer.writerow([seq, city, state])

例如,thirs文件的输出应为

NewSeqNo   City           State
002   Chicago         IL
004   Boston          MA
005   New York    NY
003   Los Angeles CA
001   Seattle         WA

感谢任何帮助

3 个答案:

答案 0 :(得分:1)

将“数字”转换为整数,以便在存储到字典中之前删除填充:

import csv

lookup = {}
with open('secondfile') as f:
    reader = csv.reader(f)
    for line in reader:
        oldseq, city, state, newseq = line
        lookup[int(oldseq)] = newseq


with open('firstfile') as f, open('outfile','w') as w:
    reader = csv.reader(f)
    writer = csv.writer(w)
    for line in reader:
        seq, city, state = line
        if int(seq) in lookup:
            seq = lookup[int(seq)]
        writer.writerow([seq, city, state])

现在lookup有整数键,当在第二个循环中查找匹配键时,我们再次传入整数键。

答案 1 :(得分:0)

如果你知道它总是被填充为3,那么在阅读你的第一个文件时,你可以将seq转换为int并使用格式来填写填充值:

with open('firstfile') as f, open('outfile','w') as w:
    reader = csv.reader(f)
    writer = csv.writer(w)
    for line in reader:
        seq, city, state = line

        # Convert to padded value
        seq = "{:03}".format(int(seq))

        if seq in lookup:
            seq = lookup[seq]
        writer.writerow([seq, city, state])

答案 2 :(得分:0)

#!/usr/bin/python

old_dict = dict()
new_dict = dict()
with open('old', 'r') as fh:
    for l in fh.readlines():
        r = l.split()
        if r:
            old_dict.setdefault(int(r[0]), None)
            old_dict[int(r[0])] = ' '.join(r[1:])

with open('new', 'r') as fh:
    for l in fh.readlines():
        r = l.split()
        if r:
            k = ' '.join(r[1:-1])
            new_dict.setdefault(k, None)
            new_dict[k] = int(r[-1])
for i,j in old_dict.iteritems():
    d = j.split()
    print '%0.3d %s %s' % (new_dict[j], ' '.join(d[0:-1]), d[-1])

输出:

 002 Chicago IL
 004 Boston MA
 005 New York NY
 003 Los Angeles CA
 001 Seattle WA
相关问题