Chinaunix首页 | 论坛 | 博客
  • 博客访问: 259013
  • 博文数量: 84
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 927
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-06 23:00
个人简介

growing

文章分类

全部博文(84)

文章存档

2017年(6)

2016年(61)

2015年(17)

我的朋友

分类: Python/Ruby

2017-03-06 19:48:58

文件1为key,文件2为key-value(两列),从文件1读取key,然后从文件2中找出value后叠加

  1. import re$
  2. import urllib2$
  3. def itercolumn23(filename, splitregex = '\t'):$
  4.     with open(filename, 'rt') as handle:$
  5.         for ln in handle:$
  6.             items = re.split(splitregex, ln)$
  7.             yield items[0], items[1]$//返回文件第一列和第二列
  8. $
  9. ............$
  10. if __name__ == '__main__':.$
  11.     a = [str(l.split()[0]) for l in open("nometa")]$ //把只有一列的txt文件转化成str类型的list
  12.     for x, y in itercolumn23("有两列的文件",splitregex = '\s+'):$
  13.         //x为第一列,y为第二列
  14.         if x in a:$
  15.             print y$

阅读(3813) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~