首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

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

2012-09-14 
[d]python怎么对应编号写入内容文件1:test.txt,内容如下:编号名称1中N2中S3上N4上S5下N6下S文件2:room.txt

[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() 

热点排行