伙计们,我的字典历史记录每次都会被覆盖,因此我希望它为字典添加新条目。
def pHistory(): #Creates/loads a Dict of historical prices then saves it after the update phase 2
global phistory
global dataentries
phistory = {}
dataentries = 0
print phistory
print dataentries
def loaddata():
global dataentriesa
with open('StockDb.FUMatt','r') as y:
vhistory = y.read()
print vhistory
phistory = vhistory
print phistory
with open('count.FUMatt','r') as ya:
strdataa = ya.read()
print strdataa
dataentriesa = int(strdataa)
print dataentriesa
def save(): #function to save database to file
with open('StockDb.FUMatt','w') as z:
savith = json.dumps(phistory)
z.write(savith)
print savith
with open('count.FUMatt','w') as j:
strdata = str(dataentries)
j.write(strdata)
print strdata
loaddata()
dataentries = dataentriesa
a = stockdf.keys()
print dataentries
print phistory
for b in a:
name = stockdf[b]['name']
price = stockdf[b]['price']
time = stockdf[b]['time']
historyupdate = { b: {dataentries: {time:price}}}
phistory.update(historyupdate)
c = phistory.keys()
#print c
dataentries = dataentries + 1
save()
运行此代码时,计数对于日志条目而言工作正常,但是如何说将dataentrie 2添加到dict时,数据entry 1中的所有信息都会被覆盖。我在俯视什么吗?
json格式为
{
"EVL": {
"35": {
"2018-08-13 14:00:01": "440.716"
}
},
"TCHS": {
"35": {
"2018-08-13 14:00:01": "994.678"
}
},
"FHG": {
"35": {
"2018-08-13 14:00:01": "404.863"
}
},
"WLT": {
"35": {
"2018-08-13 14:00:01": "988.793"
}
},
"BAG": {
"35": {
"2018-08-13 14:00:01": "2346.422"
}
},
"MSG": {
"35": {
"2018-08-13 14:00:01": "305.658"
}
},
"TMI": {
"35": {
"2018-08-13 14:00:01": "1021.110"
}
},
"TGP": {
"35": {
"2018-08-13 14:00:01": "195.230"
}
},
"IOU": {
"35": {
"2018-08-13 14:00:01": "535.950"
}
},
"LSC": {
"35": {
"2018-08-13 14:00:01": "989.317"
}
},
"TSBC": {
"35": {
"2018-08-13 14:00:01": "496.127"
}
},
"PRN": {
"35": {
"2018-08-13 14:00:01": "851.644"
}
},
"TCP": {
"35": {
"2018-08-13 14:00:01": "242.966"
}
},
"TCM": {
"35": {
"2018-08-13 14:00:01": "553.274"
}
},
"WSSB": {
"35": {
"2018-08-13 14:00:01": "65.845"
}
},
"IIL": {
"35": {
"2018-08-13 14:00:01": "85.831"
}
},
"TCB": {
"35": {
"2018-08-13 14:00:01": "428.470"
}
},
"TCC": {
"35": {
"2018-08-13 14:00:01": "550.622"
}
},
"EWM": {
"35": {
"2018-08-13 14:00:01": "871.508"
}
},
"YAZ": {
"35": {
"2018-08-13 14:00:01": "51.068"
}
},
"TCSE": {
"35": {
"2018-08-13 14:00:01": "19067.656"
}
},
"CNC": {
"35": {
"2018-08-13 14:00:01": "642.367"
}
},
"GRN": {
"35": {
"2018-08-13 14:00:01": "267.374"
}
},
"SLAG": {
"35": {
"2018-08-13 14:00:01": "234.068"
}
},
"HRG": {
"35": {
"2018-08-13 14:00:01": "426.689"
}
},
"ISTC": {
"35": {
"2018-08-13 14:00:01": "1010.354"
}
},
"SYM": {
"35": {
"2018-08-13 14:00:01": "1184.627"
}
},
"SYS": {
"35": {
"2018-08-13 14:00:01": "660.124"
}
},
"ELBT": {
"35": {
"2018-08-13 14:00:01": "125.758"
}
},
"MCS": {
"35": {
"2018-08-13 14:00:01": "1720.868"
}
},
"TCT": {
"35": {
"2018-08-13 14:00:01": "375.333"
}
}
} {
"EVL": {
"36": {
"2018-08-13 14:15:01": "440.425"
}
},
"TCHS": {
"36": {
"2018-08-13 14:15:01": "994.438"
}
},
"FHG": {
"36": {
"2018-08-13 14:15:01": "404.876"
}
},
"WLT": {
"36": {
"2018-08-13 14:15:01": "988.940"
}
},
"BAG": {
"36": {
"2018-08-13 14:15:01": "2348.502"
}
},
"TCT": {
"36": {
"2018-08-13 14:15:01": "375.416"
}
},
"MSG": {
"36": {
"2018-08-13 14:15:01": "305.709"
}
},
"TMI": {
"36": {
"2018-08-13 14:15:01": "1021.672"
}
},
"TGP": {
"36": {
"2018-08-13 14:15:01": "195.230"
}
},
"LSC": {
"36": {
"2018-08-13 14:15:01": "989.401"
}
},
"TSBC": {
"36": {
"2018-08-13 14:15:01": "496.085"
}
},
"PRN": {
"36": {
"2018-08-13 14:15:01": "851.735"
}
},
"TCP": {
"36": {
"2018-08-13 14:15:01": "242.994"
}
},
"TCM": {
"36": {
"2018-08-13 14:15:01": "552.139"
}
},
"WSSB": {
"36": {
"2018-08-13 14:15:01": "65.845"
}
},
"IIL": {
"36": {
"2018-08-13 14:15:01": "85.833"
}
},
"TCB": {
"36": {
"2018-08-13 14:15:01": "428.538"
}
},
"TCC": {
"36": {
"2018-08-13 14:15:01": "550.983"
}
},
"EWM": {
"36": {
"2018-08-13 14:15:01": "871.608"
}
},
"YAZ": {
"36": {
"2018-08-13 14:15:01": "51.065"
}
},
"TCSE": {
"36": {
"2018-08-13 14:15:01": "19069.714"
}
},
"CNC": {
"36": {
"2018-08-13 14:15:01": "642.603"
}
},
"GRN": {
"36": {
"2018-08-13 14:15:01": "267.333"
}
},
"SLAG": {
"36": {
"2018-08-13 14:15:01": "233.866"
}
},
"HRG": {
"36": {
"2018-08-13 14:15:01": "426.800"
}
},
"ISTC": {
"36": {
"2018-08-13 14:15:01": "1010.045"
}
},
"SYM": {
"36": {
"2018-08-13 14:15:01": "1184.777"
}
},
"SYS": {
"36": {
"2018-08-13 14:15:01": "660.208"
}
},
"ELBT": {
"36": {
"2018-08-13 14:15:01": "125.717"
}
},
"MCS": {
"36": {
"2018-08-13 14:15:01": "1721.041"
}
},
"IOU": {
"36": {
"2018-08-13 14:15:01": "535.890"
}
}
}
这是您的附加更改,在其中肯定会取得进展,但最佳的情况是不会将它们分开,因为这样只会将他们分开购买计数
counts格式实际上只是一个整数,例如现在在
36 但从技术上讲,已经将其增加到37,以便下次运行历史记录时会加载37,并将其加载到数据条目中
{
"EVL": {
"35": {
"2018-08-13 14:00:01": "440.716"
}
"36": {
"2018-08-13 14:00:01": "440.716"
}
},
"TCHS": {
"35": {
"2018-08-13 14:00:01": "994.678"
}
"36": {
"2018-08-13 14:00:01": "440.716"
}
},
这是所需的格式
我认为我可以将b插入
之类的东西中b = u'EVL'
import unicodedata
unicodedata.normalize('NFKD', b).encode('ascii','ignore')
'EVL'
答案 0 :(得分:2)
检查是否可行
for b in a:
name = stockdf[b]['name']
price = stockdf[b]['price']
time = stockdf[b]['time']
if not b in phistory:
phistory[b] = {}
phistory[b][dataentries] = {time:price}
c = phistory.keys()
#print c
dataentries = dataentries + 1
save()
这应该会导致您正在寻找的行为。
请在数据库的较小样本上运行测试,这将使调试更快。
有趣的是,由于每次调用该函数时都要重写/更新历史记录,因此实际上我们可能希望在这里使用“ w”而不是“ a”。否则,我们基本上将得出存储在一个文件中的每次历史记录迭代的历史记录,这可能会变得很庞大。我建议进行上述更改,然后分别测试“ w”和“ a”,因为我认为“ w”实际上可以为您提供所需的信息。
所以这就是您创建字典并对其进行更新的方式。 update()会如其所言,在字典中更新(k,v)对。除非它们不存在,否则它不会附加任何东西。由于顶级键“ EVL”已经存在,因此当您传入update({'EVL':some_values})
时,其所有值都将被覆盖。我这样做的方法是创建一个空条目(如果没有的话),然后通过基本告诉它在新密钥中输入什么值来更新它。语法
dict[key] = X
将更新该密钥,或者如果该密钥不存在,则创建一个新密钥,但是
dict[key][key2] = X
依赖于现有的dict [key],因为否则它将不知道[key2]的父级将首先添加它。这就是为什么我们必须显式创建顶级密钥(如果它不存在)的原因。然后之后
dict[key_that_exists][key2] = X
将更新(key2,v)对,或者如果不存在则创建对。
希望有帮助:)