python字符串处理,去掉日期时间模式

时间:2013-05-26 23:16:30

标签: python design-patterns datetime

Python处理问题 - 删除日期时间模式:

我从以下形式的GSM单元获得了一些数据:

+CMGL: 1,"REC READ","+111111111111","13/05/25,05:15:16+04",25-05-13,05:15:20, 0.668
+CMGL: 2,"REC READ","+111111111111","13/05/25,12:15:14+04",25-05-13,12:15:20, 0.875
+CMGL: 3,"REC READ","+111111111111","13/05/25,10:15:15+04",25-05-13,10:15:20, 0.679

...

数据作为单个字符串缓冲区被检索,因此它最初都在一行上。 我可以使用DATA.replace(a,b)对数据进行排序和删除,但是我无法删除前4个逗号分隔的组, 即

+CMGL: 1,"REC READ","+111111111111","YY/MM/DD,HH:MM:SS+DELTA"

我的目标是提取数据看起来像这样(我不介意日期时间行的错误顺序) -

25-05-13, 05:15:20, 0.668
25-05-13, 12:15:20, 0.875
25-05-13, 10:15:20, 0.679

...

欢迎提出建议

4 个答案:

答案 0 :(得分:2)

使用csv模块处理分隔文件。

<强> gsm.txt

+CMGL: 1,"REC READ","+111111111111","13/05/25,05:15:16+04",25-05-13,05:15:20, 0.668
+CMGL: 2,"REC READ","+111111111111","13/05/25,12:15:14+04",25-05-13,12:15:20, 0.875
+CMGL: 3,"REC READ","+111111111111","13/05/25,10:15:15+04",25-05-13,10:15:20, 0.679

下面的示例代码

import csv
gsm = open('gsm.txt')
for row in csv.reader(gsm):
    print row[4:]

输出

['25-05-13', '05:15:20', ' 0.668']
['25-05-13', '12:15:20', ' 0.875']
['25-05-13', '10:15:20', ' 0.679']

答案 1 :(得分:1)

这样的事情:

>>> strs = '+CMGL: 1,"REC READ","+111111111111","13/05/25,05:15:16+04",25-05-13,05:15:20, 0.668'
>>> ", ".join( x for x in strs.split(",")[5:] )
'25-05-13, 05:15:20,  0.668'

或:

>>> ", ".join( strs.split(",",5)[-1].split(",") )
'25-05-13, 05:15:20,  0.668'

对于多行:

>>> strs = """+CMGL: 1,"REC READ","+111111111111","13/05/25,05:15:16+04",25-05-13,05:15:20, 0.668                                              
+CMGL: 2,"REC READ","+111111111111","13/05/25,12:15:14+04",25-05-13,12:15:20, 0.875
+CMGL: 3,"REC READ","+111111111111","13/05/25,10:15:15+04",25-05-13,10:15:20, 0.679"""
>>> 
>>> for line in strs.splitlines():     
...     print ", ".join( line.split(",",5)[-1].split(","))

25-05-13, 05:15:20,  0.668
25-05-13, 12:15:20,  0.875
25-05-13, 10:15:20,  0.679

答案 2 :(得分:1)

data = """+CMGL: 1,"REC READ","+111111111111","13/05/25,05:15:16+04",25-05-13,05:15:20, 0.668
+CMGL: 2,"REC READ","+111111111111","13/05/25,12:15:14+04",25-05-13,12:15:20, 0.875
+CMGL: 3,"REC READ","+111111111111","13/05/25,10:15:15+04",25-05-13,10:15:20, 0.679"""

import csv
from StringIO import StringIO

for row in csv.reader(StringIO(data), skipinitialspace=True):
    print ', '.join(row[4:7])

#25-05-13, 05:15:20, 0.668
#25-05-13, 12:15:20, 0.875
#25-05-13, 10:15:20, 0.679

答案 3 :(得分:0)

如果你能确保所有的行都是类似的格式,那么前缀词的长度总是相同的。我认为最简单的方法是

line = '+CMGL: 1,"REC READ","+111111111111","13/05/25,05:15:16+04",25-05-13,05:15:20, 0.668'
line = line[59:]