插入' null'在订单重要时将值写入字典

时间:2017-09-06 22:24:23

标签: python datetime dictionary

我有list名为entries,由多个字符串组成。

  

entries中的每个条目都有数据,   closeloop txup cycletxdown cyclerxup cyclerxdown cycle
  随后是时间,日期来自条目中的其他地方   (由dtdict中的' utdate'照顾。

entries中每个条目的顺序很重要,我需要更新我的字典d,并将日期/时间更新为UTC,但每个&#39 ;闭环'条目我需要字典中的相应值为“空”'这就是我所拥有的:

import re
from datetime import datetime
import pytz

rawdates = []
converted_dt = []
converted_txup = []
local = pytz.timezone("Etc/GMT+4")
d = {}
dtdict = {'txup': '#regexpattern',
         'txdown': '#regexpattern',
         'rxup': '#regexpattern',
         'rxdown': '#regexpattern',
         'utdate': '#regexpattern'}

cycle_entries = [e for e in entries if 'cycle' in e]
clloop_entries = [e for e in entries if 'closeloop' in e]

txupmatches = re.findall(dtdict['txup'], str(entries))
txdownmatches = re.findall(dtdict['txdown'], str(entries))
rxupmatches = re.findall(dtdict['rxup'], str(entries))
rxdownmatches = re.findall(dtdict['rxdown'], str(entries))

for entry in cycle_entries:
    dates = datetime.strptime(re.search(dtdict['utdate'], entry).group(0), '%c')
    localdatestr = str(dates.strftime('%Y-%m-%d'))
    rawdates.append(localdatestr)
for i in range(len(cycle_entries)):
    local_time = datetime.strptime(rawdates[i]+' '+txupmatches[i], '%Y-%m-%d %X')
    localized_time = local.localize(local_time)
    utc_time_str = str(localized_time.astimezone(pytz.utc).strftime('%Y-%m-%d %X')
    converted_txup.append(utc_time_str)
for entry in entries:
    d.update({'txup': converted_txup if re.search(dtdict['txup'], entry) else 'NULL'})

但这并不是我想要的 - 当我打印d['txup']时,我需要一个值列表,其中包含每个周期的转换日期时间字符串'输入和值' NULL'对于每个' closeloop'每个条目的顺序与entries中的条目相同。有谁知道怎么处理这个问题?

我还需要添加额外的代码行来为' txdown',' rxup'和' rxdown&#39做同样的事情;所以,如果有更有效的方式做同样的事情,请告诉我。

0 个答案:

没有答案