2013-09-30 36 views
1

我具有存储温度的格式的日志文件的JSON数组:使用Python正则表达式来创建温度数据

2013/09/30 11:23:01 Temperature 41.34F 5.19C 
2013/09/30 11:23:01 Temperature 99.84F 37.69C 
2013/09/30 11:23:01 Temperature 65.86F 18.81C 
2013/09/30 11:25:02 Temperature 41.67F 5.38C 
2013/09/30 11:25:02 Temperature 65.64F 18.69C 
2013/09/30 11:25:02 Temperature 98.83F 37.12C 

有对应于给定分钟值的可变数目,1-3个。我如何使用Python正则表达式将数据转换为JSON格式,以便为每个时间和华氏值赋予一系列值?

{"c":[{"v":"Date(2013, 8, 30, 11, 23)"},{"v":41.34},{"v":99.84},{"v":65.86}]}, 

所以脚本将打开“temperatures.log”,通过文件读取,花时间值,并把它的格式为:

{"c":[{"v":"Date(2013, 8, 30, 11, 23)"}, 

(与月-1偏移)

,然后循环通过此时的所有温度值和包括每个这样的:

{"v":41.34}, 

直到它发现,是从上一行不同的日期/时间表达,然后关闭该表达与

]}, 

写入输出文件,并开始下一次系列,直到日志文件的末尾。

+1

这几乎是老生常谈,但仍然!“当面对一个问题,程序员会想,“啊哈,我将使用一个正则表达式!'而现在他有两个问题。“ - 正则表达式是一个非常有效的工具,但它有一个非常特殊的地方。就像你在工具箱背后拥有的一种专业工具,除了它的一个预期目的外根本就没有任何用处。 –

回答

3

你不需要正则表达式,因为你的数据非常简单。首先,请注意,您可以组织数据,甚至没有解析日期,因为你可以用简单的字符串比较:

def proc_lines(lines): 
    cur_date = None 
    cur_temps = [] 

    results = [] 

    for line in lines: 
     parts = line.split() 
     date = "%s %s" % (parts[0], parts[1]) 
     if date != cur_date: 
      if cur_temps: 
       #save current data 
       results.append((cur_date, cur_temps)) 
      #reset state 
      cur_date = date 
      cur_temps = [] 
     #add the line's temperature in fahrenheit, stripping out the 'F' 
     cur_temps.append(float(parts[3][:-1])) 

    #process the last line 
    if cur_temps: 
     results.append((cur_date, cur_temps)) 

    return results 

现在results将是(date, temperature)元组未解析日期列表:

>>> lines = """2013/09/30 11:23:01 Temperature 41.34F 5.19C 
2013/09/30 11:23:01 Temperature 99.84F 37.69C 
2013/09/30 11:23:01 Temperature 65.86F 18.81C 
2013/09/30 11:25:02 Temperature 41.67F 5.38C 
2013/09/30 11:25:02 Temperature 65.64F 18.69C 
2013/09/30 11:25:02 Temperature 98.83F 37.12C""".split("\n") 
>>> results = proc_lines(lines) 
>>> results 
[('2013/09/30 11:23:01', [41.340000000000003, 99.840000000000003, 
          65.859999999999999]), 
('2013/09/30 11:25:02', [41.670000000000002, 65.640000000000001, 
          98.829999999999998])] 

您可以使用datetime.datetime.strptime实际解析日期和处理日期(减去每月你问):

>>> import datetime 
>>> def proc_datestr(date): 
     dt = datetime.datetime.strptime(date, "%Y/%m/%d %H:%M:%S") 
    return "Date(%d, %d, %d, %d, %d, %d)" % (
     dt.year, dt.month - 1, dt.day, dt.hour, dt.minute, dt.second) 

>>> proc_datestr(results[0][0]) 
'Date(2013, 8, 30, 11, 23, 1)' 

请注意,格式字符串"%Y/%m/%d %H:%M:%S"分析日期as detailed here。这个可爱的内置函数可以避免您编写自己的正则表达式来处理日期。

然后你只需处理结果&转储到JSON如下:

>>> import json 
>>> def proc_result(result): 
    date, temps = result 
    res = {'c': [{'v': proc_datestr(date)}]} 
    for temp in temps: 
     res['c'].append({'v': temp}) 
    return json.dumps(res) 

>>> proc_result(results[0]) 
'{"c": [{"v": "Date(2013, 8, 30, 11, 23, 1)"}, {"v": 41.340000000000003}, {"v": 99.840000000000003}, {"v": 65.859999999999999}]}' 
>>> proc_result(results[1]) 
'{"c": [{"v": "Date(2013, 8, 30, 11, 25, 2)"}, {"v": 41.670000000000002}, {"v": 65.640000000000001}, {"v": 98.829999999999998}]}'