python 处理不同数据格式-进行排序处理

 

如:针对以下数据,对数字进行排序:

 

1 2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22
2 2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21
3 2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38
4 2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55

 

使用以下程序进行排序:

 1 #!/usr/bin/env python
 2 
 3 with open('james.txt') as jaf:
 4 
 5     data = jaf.readline()
 6 james = data.strip().split(',')
 7 with open('julie.txt') as juf:
 8 
 9     data = juf.readline()
10 
11 julie = data.strip().split(',')
12 
13 with open('mikey.txt') as mif:
14 
15     data = mif.readline()
16 mikey = data.strip().split(',')
17 with open('sarah.txt') as saf:
18 
19     data = saf.readline()
20 sarah = data.strip().split(',')
21 
22 def check_str(time_string):
23 
24     if '-' in time_string:
25        splitter = '-'
26     elif ':' in time_string:
27        splitter = ':'
28     else:
29        return time_string
30     (mins,secs) = time_string.split(splitter)
31     return (mins + '.' + secs)
32 
33 print sorted([check_str(each) for each in james])
34 print sorted([check_str(each) for each in julie])
35 print sorted([check_str(each) for each in mikey])
36 print sorted([check_str(each) for each in sarah])

 

posted @ 2014-12-25 22:35  marcwang  阅读(389)  评论(1)    收藏  举报