免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-11-17 11:08 |只看该作者 |倒序浏览
现有文件a.txt:
xudb@asg.com
fengjw@asg.com
lusixi@asg.com
...
文件b.txt:
schedule = no_phone | online | offline | absence
mobile-phone = no_plan | meeting | away | biz_trip |bye-bye
location = china | japan | america | england
希望随机地组合成c.txt
xudb@asg.com   schedule          online
xudb@asg.com   mobile-phone   no_plan
xudb@asg.com   location            china

fengjw@asg.com  schedule          online
fengjw@asg.com  mobile-phone  away
fengjw@asg.com  location            england

... ...
说明:c.txt中 第三个值是要随机生成的。 如果第二个值是schedule 那么第三个值是no_phone | online | offline | absence中的一个,如果第二个值是mobile-phone那么第三个值是no_plan | meeting | away | biz_trip | bye-bye中的一个。谢谢,各位!

论坛徽章:
0
2 [报告]
发表于 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
3 [报告]
发表于 2003-11-17 11:44 |只看该作者

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

谢谢, CharlesWang2
我在linux上用不好使,语法错误!
能不能不用Python呢? 谢谢!!!

论坛徽章:
0
4 [报告]
发表于 2003-11-17 13:50 |只看该作者

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

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

Python 2.0 以上才行

论坛徽章:
0
5 [报告]
发表于 2003-11-17 14:33 |只看该作者

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

xiexie!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP