免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1445 | 回复: 1
打印 上一主题 下一主题

两个文件的组合问题,急! [复制链接]

论坛徽章:
0
1 [报告]
发表于 2003-11-17 11:31 |显示全部楼层

两个文件的组合问题,急!

用 Python

  1. %cat b.txt
  2. schedule = no_phone | online | offline | absence
  3. mobile-phone = no_plan | meeting | away | biz_trip |bye-bye
  4. location = china | japan | america | england
  5. %cat t.py
  6. import sys,os,random
  7. a=open("a.txt")
  8. b=open("b.txt")
  9. lines_a = a.readlines()
  10. lines_b = b.readlines()
  11. header= [ (s.split("=")[0].strip(), s.split("=")[1].split('|')) for s in lines_b]

  12. for x in lines_a:
  13.     for h in header:
  14.         c = " ".join((x[:-1], h[0],h[1][random.randrange(len(h[1]))]))
  15.         c = c.replace('\n','')
  16.         print c


  17. %python t.py
  18. xudb@asg.com schedule  online
  19. xudb@asg.com mobile-phone  no_plan
  20. xudb@asg.com location  america
  21. fengjw@asg.com schedule  no_phone
  22. fengjw@asg.com mobile-phone bye-bye
  23. fengjw@asg.com location  england
  24. lusixi@asg.com  schedule  offline
  25. lusixi@asg.com  mobile-phone  away
  26. lusixi@asg.com  location  japan
复制代码

论坛徽章:
0
2 [报告]
发表于 2003-11-17 13:50 |显示全部楼层

两个文件的组合问题,急!

Python 1.5 不行,没有 list comprehension , 太老了。

Python 2.0 以上才行
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP