我正在编写代码以对某些股票进行分析,并将所有数据存储在字典中。当我遍历“ .csv”文件时,print语句给出了正确的输出,但是使用与print语句相同的变量的字典却很奇怪。 代码:
def checkDbForReqData(self):
for stock in self.stocks:
self.cursor.execute(query.selectQuery("*",stock))
data = self.cursor.fetchall()
if(len(data) < 20):
print(" Not enough data for: %s" % (stock))
self.downloadData(stockName=stock,condition=True)
x = datetime.date.today()
fromDate = x.strftime("%d") + "-" + x.strftime("%m") + "-" + x.strftime("%Y")
toDate = (x - datetime.timedelta(days = 25)).strftime("%d") + "-" + (x - datetime.timedelta(days = 25)).strftime("%m") + "-" + (x - datetime.timedelta(days = 25)).strftime("%Y")
fileName = str(toDate) + "-TO-" + str(fromDate) + stock + "EQN.csv"
with open("../../fileDump/"+fileName) as theFile:
print(" Opened: " + fileName)
reader = csv.DictReader(theFile)
finalDict = {}
localDict = {}
#The part where I'm running into problems
for row in reader:
print(row['Series'],row['Date'],row['Turnover'],row['Total Traded Quantity'],(float(row['Turnover'])/float(row['Total Traded Quantity'])))
localDict.update({"Series" : row['Series']})
localDict.update({"Turnover" : row['Turnover']})
localDict.update({"Total Traded Quantity" : row['Total Traded Quantity']})
localDict.update({"VWAP" : (float(row['Turnover'])/float(row['Total Traded Quantity']))})
finalDict.update({row["Date"] : localDict})
print(json.dumps(finalDict))
print语句给出正确的输出:
Opened: 03-02-2019-TO-28-02-2019BELEQN.csv
EQ 04-Feb-2019 311906105.50 3762635 82.89565836175977
EQ 05-Feb-2019 427175618.30 5285262 80.82392477421176
EQ 06-Feb-2019 506919711.85 6294987 80.52752322602096
EQ 07-Feb-2019 440903683.10 5396444 81.7026329004804
EQ 08-Feb-2019 279516827.60 3458045 80.83088207354156
EQ 11-Feb-2019 622475430.75 8090201 76.94189931127792
EQ 12-Feb-2019 686804293.65 8781184 78.21317645205932
EQ 13-Feb-2019 693049047.25 9081481 76.3145402440417
EQ 14-Feb-2019 1330591632.15 17860130 74.50066892850164
EQ 15-Feb-2019 752767295.85 10021141 75.11792278444142
EQ 18-Feb-2019 309558993.45 4102420 75.457655103573
EQ 19-Feb-2019 433956845.90 5698562 76.15199166035221
EQ 20-Feb-2019 969747107.30 12525521 77.42169825111466
EQ 21-Feb-2019 375559870.05 4870359 77.11133204964973
EQ 22-Feb-2019 816862761.70 10396045 78.57437724634705
EQ 25-Feb-2019 1742414944.05 22009380 79.16692537681661
EQ 26-Feb-2019 1209307010.10 15257415 79.26028164666164
EQ 27-Feb-2019 1157652436.70 14278303 81.07773288604396
但是我的字典看起来像这样:
{
"04-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"05-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"06-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"07-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"08-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"11-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"12-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"13-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"14-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"15-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"18-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"19-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"20-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"21-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"22-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"25-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"26-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
},
"27-Feb-2019": {
"Series": "EQ",
"Turnover": " 1157652436.70",
"Total Traded Quantity": " 14278303",
"VWAP": 81.07773288604396
}
}
键具有正确的值,但dict_values都相同,我在这里看不到错误之处。 我很抱歉,如果这是一个非常基本的错误。