python分区获取值

时间:2015-11-27 15:44:28

标签: python string

继续Getting a list and values,好了,现在格式化,然后写入文件或表格。 我已经尝试过重新分割和分区按摩数据到我需要的东西但我不确定哪个最适合我的需要。我仍然需要将mac地址与外部文件匹配以获得用户友好名称,或者可能在程序中应该更进一步。我需要对数值进行数学计算,但仍然需要匹配mac地址:这就是我所拥有的,我是朝着正确的方向前进还是我错过了什么?

    for item in parsed_json['list']:
   # print(item['timestamp']), (item['id']), (item['value'])
  #ts = datetime.datetime.strptime((item['timestamp']), "%Y-%m-%dT%H:%M:%S.%fZ")
  ts = datetime.datetime.strptime((item['timestamp']), '%Y-%m-%dT%H:%M:%S.%fZ')
  sts = datetime.datetime.utcnow()
  delta = sts-ts
  time_diff = str(delta).split('.')[0]
  frmt_ts = datetime.datetime.strftime(ts, '%m/%d/%y %H:%M:%S')
  frmt_part_id = (item['id']).partition("/[")[2]
  frmt_part2_id = (frmt_part_id).partition("]!/")[0] 
  frmt_part3_id = (frmt_part_id).partition("]!/")[2]



  print frmt_ts, frmt_part2_id, frmt_part3_id, (item['value']), "Time difference is:  ", time_diff

产生的输出如下:

11/27/15 15:18:59 00:13:A2:00:40:B5:8A:35 AD1 419 Time difference is:   0:00:11
11/27/15 15:18:59 00:13:A2:00:40:B5:8A:35 AD2 663 Time difference is:   0:00:11
11/27/15 15:18:59 00:13:A2:00:40:B5:8A:3A AD1 63 Time difference is:   0:00:11
11/27/15 15:18:59 00:13:A2:00:40:B5:8A:3A AD2 641 Time difference is:   0:00:11
11/27/15 15:19:00 00:13:A2:00:40:B5:8A:3B AD1 14 Time difference is:   0:00:11
11/27/15 15:18:59 00:13:A2:00:40:B5:8A:3B AD2 639 Time difference is:   0:00:11
11/27/15 15:19:00 00:13:A2:00:40:B7:AE:D6 AD1 4 Time difference is:   0:00:10
11/27/15 15:19:00 00:13:A2:00:40:B7:AE:D6 AD2 618 Time difference is:   0:00:11
11/26/15 13:51:58 00:13:A2:00:40:D5:8F:0A AD1 10 Time difference is:   1 day, 1:27:12
11/26/15 13:51:58 00:13:A2:00:40:D5:8F:0A AD2 685 Time difference is:   1 day, 1:27:12
11/26/15 13:51:58 00:13:A2:00:40:D5:8F:0A AD3 853 Time difference is:   1 day, 1:27:12
11/26/15 18:30:28 00:13:A2:00:40:D5:8F:50 AD1 2 Time difference is:   20:48:42
11/26/15 18:30:29 00:13:A2:00:40:D5:8F:50 AD2 712 Time difference is:   20:48:42
11/26/15 18:30:28 00:13:A2:00:40:D5:8F:50 AD3 13 Time difference is:   20:48:42
11/27/15 15:19:01 00:13:A2:00:40:D5:8F:5B AD1 28 Time difference is:   0:00:10
11/27/15 15:19:01 00:13:A2:00:40:D5:8F:5B AD2 602 Time difference is:   0:00:09
11/27/15 15:19:00 00:13:A2:00:40:D5:8F:5B AD3 445 Time difference is:   0:00:10
11/27/15 13:35:35 00:13:A2:00:40:D5:8F:6F AD1 7 Time difference is:   1:43:35

1 个答案:

答案 0 :(得分:0)

import re

mac_re = re.compile(r'([\dA-F]{2}:){7}[\dA-F]{2}', re.I) # use re.I or not, case-sensitive
processed_items = []
for item in parsed_json['list']:
    processed_item = {}
    # do some process, you should consider exceptions
    _s = mac_re.search(item.get('id', ''))
    processed_item['mac'] = _s.group() if _s else ''
    processed_item['value'] = item.get('value')
    # append the processed_item
    processed_items.append(processed_item)
# do some other process with those processed_items

=========一些解释=========

([\dA-F]{2}:){7}[\dA-F]{2}这是Regular Expression

  • \d匹配单个号码0-9
  • A-F匹配AF
  • 中的单个字符
  • [\dA-F]匹配单个字符,可以是0-9A-F
  • [\dA-F]{2}匹配两次
  • ([\dA-F]{2}:){7}匹配[\dA-F]{2} 7次

因此,([\dA-F]{2}:){7}[\dA-F]{2}匹配字符串

中显示的mac地址

忽略大小写的re.I标记集,表示A可以匹配a