读书人

[d]python如何对应编号写入内容

发布时间: 2012-09-14 11:53:44 作者: rapoo

[d]python怎么对应编号写入内容
文件1:test.txt,内容如下:
编号 名称
1 中N
2 中S
3 上N
4 上S
5 下N
6 下S

文件2:room.txt, 内容如下:
编号 名称
1
2
6

1
2
6

1
2
6

如何对应编号,将文件1中的名称写入文件2中??

--------------------------
Double行动:
原帖分数:40
帖子加分:40


[解决办法]

Python code
#!/usr/bin/env pythonaDict = {}with open('test.txt') as fd:    for line in fd:        x, y = line.split()        aDict[x] = ywfd = open('room.tmp', 'w')with open('room.txt') as fd:    for line in fd:        num = line.strip()        if num:            print >>wfd, num, aDict[num]        else:            print >>wfd, linewfd.close() 

读书人网 >perl python

热点推荐