免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 7939 | 回复: 13

[求助]txt文件排序 [复制链接]

论坛徽章:
0
发表于 2009-07-14 16:26 |显示全部楼层
一txt文件,里面的记录如下,现在想根据第一列时间来排序成
  1. 2009-06-24,0000002001,0802,1201,1452,1800
复制代码


请各位CU兄弟帮忙,谢谢。

  1. 2009-06-24 00:00:00,0802,0000002001,0
  2. 2009-06-24 00:00:00,1201,0000002001,0
  3. 2009-06-24 00:00:00,1452,0000002001,0
  4. 2009-06-24 00:00:00,1755,0000002001,0
  5. 2009-06-24 00:00:00,1800,0000002001,0
  6. 2009-06-25 00:00:00,0741,0000002001,0
  7. 2009-06-25 00:00:00,1202,0000002001,0
  8. 2009-06-25 00:00:00,1447,0000002001,0
  9. 2009-06-25 00:00:00,1800,0000002001,0
  10. 2009-06-26 00:00:00,0746,0000002001,0
  11. 2009-06-26 00:00:00,1200,0000002001,0
  12. 2009-06-26 00:00:00,1310,0000002001,0
  13. 2009-06-26 00:00:00,1431,0000002001,0
  14. 2009-06-26 00:00:00,1801,0000002001,0
  15. 2009-06-27 00:00:00,0803,0000002001,0
  16. 2009-06-27 00:00:00,1213,0000002001,0
  17. 2009-06-27 00:00:00,1427,0000002001,0
  18. 2009-06-27 00:00:00,1800,0000002001,0
复制代码

论坛徽章:
0
发表于 2009-07-14 17:53 |显示全部楼层
我做的,方法有点笨,不知道是不是你想要的要求.

code='''2009-06-24 00:00:00,0802,0000002001,0
2009-06-24 00:00:00,1201,0000002001,0
2009-06-24 00:00:00,1452,0000002001,0
2009-06-24 00:00:00,1755,0000002001,0
2009-06-24 00:00:00,1800,0000002001,0
2009-06-25 00:00:00,0741,0000002001,0
2009-06-25 00:00:00,1202,0000002001,0
2009-06-25 00:00:00,1447,0000002001,0
2009-06-25 00:00:00,1800,0000002001,0
2009-06-26 00:00:00,0746,0000002001,0
2009-06-26 00:00:00,1200,0000002001,0
2009-06-26 00:00:00,1310,0000002001,0
2009-06-26 00:00:00,1431,0000002001,0
2009-06-26 00:00:00,1801,0000002001,0
2009-06-27 00:00:00,0803,0000002001,0
2009-06-27 00:00:00,1213,0000002001,0
2009-06-27 00:00:00,1427,0000002001,0
2009-06-27 00:00:00,1800,0000002001,0'
''
newcode=code.replace(' ',',')
codelist=newcode.split('\n')
i=0
finalcodelist=[]
while i<len(codelist):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp=codelist[i].split(',')
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if temp[0] in finalcodelist:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i+=1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finalcodelist.append(temp[2])
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finalcodelist.append(temp[0])
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finalcodelist.append(temp[3])
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finalcodelist.append(temp[2])
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i+=1
tar=','.join(finalcodelist)
print tar.replace(',2009','\r\n2009')


最后结果:
>>>
2009-06-24,0000002001,0802,1201,1452,1755,1800

2009-06-25,0000002001,0741,1202,1447,1800

2009-06-26,0000002001,0746,1200,1310,1431,1801

2009-06-27,0000002001,0803,1213,1427,1800
>>>

论坛徽章:
0
发表于 2009-07-15 13:46 |显示全部楼层
如果格式固定,用awk吧。。

论坛徽章:
0
发表于 2009-07-15 13:46 |显示全部楼层
如果格式固定,用awk吧。。
hamlet 该用户已被删除
发表于 2009-07-15 17:01 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
发表于 2009-07-16 16:57 |显示全部楼层

LS说的对

试试这个(1.txt是你的数据文件,2.txt是排序后的文件)
。。。。。。。。
f=open('1.txt','r')
l=f.readlines()
f.close()

l.sort()

f=open('2.txt','w')
f.writelines(l)
f.close()
。。。。。。。。。。。
以前的太麻烦,小改一下。

[ 本帖最后由 cnsunus 于 2009-7-17 13:27 编辑 ]

论坛徽章:
1
摩羯座
日期:2015-01-08 14:01:55
发表于 2009-07-16 22:49 |显示全部楼层
awk -F '[ ,]+' '{if(a[$2]==0)print $2,$(NF-1);a[$2]++;if(a[$2]>=1)print $(NF-2)}'|sed -r ':A;N;/\n\s*[0-9]{4}-[0-9]{2}-[0-9]{2}\s*/!s/\n/\t/g;tA;D'|awk 'OFS=","{$1=$1;print}'

awk菜鸟过来up下,已经在shell版块求更简洁方法了。
PS:估计今后要来python混,往各位多多照顾。

论坛徽章:
1
摩羯座
日期:2015-01-08 14:01:55
发表于 2009-07-16 22:51 |显示全部楼层
郁闷,这里:P被转义了,古用:代替;

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
发表于 2009-07-17 09:11 |显示全部楼层

回复 #1 dingjeff 的帖子

看不明白,到底哪个是源数据,哪个是想要的结果?

论坛徽章:
0
发表于 2009-07-17 11:02 |显示全部楼层
原帖由 可恶的 于 2009-7-16 22:49 发表
awk -F '[ ,]+' '{if(a[$2]==0)print $2,$(NF-1);a[$2]++;if(a[$2]>=1)print $(NF-2)}'|sed -r ':A;N;/\n\s*[0-9]{4}-[0-9]{2}-[0-9]{2}\s*/!s/\n/\t/g;tA;D'|awk 'OFS=","{$1=$1;print}'

awk菜鸟过来up下 ...

这个这个,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP