我正在寻找一个替代连接的python。
我正在尝试获取当天每一秒的列表,并根据时间戳将数据加入到该列表中。到目前为止,我的是:
keys=('DRIP_ID','DESCR','OBJECT','TIMESTAMP','DRIP_R1','DRIP_R2','RT_DISP1','RT_DISP2','DAY','TIME')
键是列名
rawdata=[['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701063825','N242','N508','10','14','20150701','063825'],
['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701064327','N242','N508','10','14','20150701','064327'],
['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701085717','N242','N508','10','14','20150701','085717'],
['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701100116','N242','N508','10','14','20150701','100116'],
['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701191611','N242','N508','10','14','20150701','191611'],
['242418',"242418 Rechts.BD242418: tot Oudkarspel - - ${pijlop} N242 10 min - N508 14 min${pijlr}",'BD242418','20150701213616','N242','N508','10','14','20150701','213616']]
rawdata来自软件
sec = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59']
mm = sec
hh = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23']
timestamp=()
time = []
dictData = []
# Dictionary with all seconds (HHMMSS) in 1 day
for ih, uur in enumerate(hh):
if ih < 24:
for im, minutes in enumerate(mm):
if im < 60:
for isec, secs in enumerate(sec):
if isec < 60:
timestamp = str(uur)+str(minutes)+str(secs)
timeDict = dict()
timeDict['DRIP_ID']=""
timeDict['DESCR']=""
timeDict['OBJECT']=""
timeDict['TIMESTAMP']=""
timeDict['DRIP_R1']=""
timeDict['DRIP_R2']=""
timeDict['RT_DISP1']=""
timeDict['RT_DISP2']=""
timeDict['DAY']=""
timeDict['TIME']=timestamp
time.append(timeDict)
在这里,我在一天内完成了所有秒数并给了他们相同的键,以便于匹配
# Turn raw data into dictionary
for row in rawdata:
dictionary = dict(zip(keys, row))
dictData.append(dictionary)
然后我把rawdata转换成dict
#Join, sort off
compleet=()
for t in time:
t.update(dictData)
compleet.append(t)
print len(compleet)
print compleet[1]
然而,当我运行它时,我收到错误:
ValueError: dictionary update sequence element #0 has length 10; 2 is required
这让我相信我一次只能更新key:value对,但我不确定这是否正确。
此外:这是一个1:1的加入。 1个时间戳只能有1个测量值。 不是每天都有一个测量。 “加入”将在“时间”
答案 0 :(得分:0)
文档说:
dict.update = update(...)
D.update([E,] ** F) - &gt;没有。从dict / iterable E和F更新D.
如果E存在且具有.keys()方法,那么:对于E中的k:D [k] = E [k]
如果E存在且缺少.keys()方法,那么:对于k,v在E中:D [k] = v
在任何一种情况下,接着是:对于F中的k:D [k] = F [k]
由于dictData
是一个列表并且没有keys()
方法,for k, v in dictData: t[k]
= v
方法会运行update
并导致异常。
实际上我并不完全理解您的代码,因此我无法为此提供具体帮助。
如果您能解释代码(例如执行后的正确t
变量),我想帮助您。
答案 1 :(得分:0)
#Same result as a join, by iterating.
for iTime, t in enumerate(time):
for iData, d in enumerate(dictData):
if t['TIME'] == d['TIME']:
t.update(d)
在意识到出了什么问题并且没有加入之后,这是最好的下一步。