Python 韩元';无法将dict行写入csv
下面是我的代码:Python 韩元';无法将dict行写入csv,python,csv,dictionary,Python,Csv,Dictionary,下面是我的代码: if(row == current_account): edited_account = {'id':current_account_id,'price':current_account_price, 'amount':current_account_remainder_amount, 'type':"BUY", 'date':current_account_date}
if(row == current_account):
edited_account = {'id':current_account_id,'price':current_account_price, 'amount':current_account_remainder_amount, 'type':"BUY", 'date':current_account_date}
new_account = {'id':s, 'price':last_order_price, 'amount':last_order_amount, 'type':"SELL", 'date':last_order_date}
writer.writerow(edited_account)
writer.writerow(new_account)
print("*** EDITED ACCOUNT: " + str(edited_account) + " ***")
print("*** ADDED ACCOUNT: " + str(new_account) + " ***")
self.update_log("*** EDITED ACCOUNT: " + str(edited_account) + " ***")
self.update_log("*** ADDED ACCOUNT: " + str(new_account) + " ***")
出于某种原因,它不允许我编写在金额字段中具有指数浮动的Dict,例如1.4935380525642888e-10。但是,所有其他浮动都可以正常工作
例如,它打印出:
*** EDITED ACCOUNT: {'amount': '0.03826056483395195', 'type': 'BUY', 'price': 1395.38, 'id': '46ce341a-dfae-40c3-9815-a2f7bebc0810', 'date': '1430324015'} ***
*** ADDED ACCOUNT: {'amount': 0.0015329392870640016, 'type': 'SELL', 'price': 1395.59, 'id': '703890df-cf1d-4ae0-aab9-0b6be357df25', 'date': '1430327386'} ***
当我检查我的csv文件时,这两个帐户都在那里
然而,在一个案例中,它打印出:
*** EDITED ACCOUNT: {'amount': '0.00010000055698286583', 'type': 'BUY', 'price': 1394.95, 'id': "<class 'str'>", 'date': '1430341136'} ***
*** ADDED ACCOUNT: {'amount': 2.3518644466467972e-08, 'type': 'SELL', 'price': 1396.41, 'id': '879aac41-0cc2-406f-a9e7-c73f7476e9de', 'date': '1430341892'} ***
***编辑的帐户:{'amount':'0.0001000055698286583','type':'BUY','price':1394.95,'id':'','date':'1430341136'}***
***新增账户:{'amount':2.3518644466467972e-08,'type':'SELL','price':1396.41,'id':'879aac41-0cc2-406f-a9e7-c73f7476e9de','date':'1430341892'}***
当我只检查我的csv文件时
{'amount': '0.00010000055698286583', 'type': 'BUY', 'price': 1394.95, 'id': "<class 'str'>", 'date': '1430341136'}
{'amount':'0.0001000055698286583','type':'BUY','price':1394.95,'id':'','date':'1430341136'}
有没有
这很奇怪,因为我编写了一个简单的测试脚本,将{'amount':2.3518644466467972e-08,'type':'SELL','price':1396.41,'id':'879aac41-0cc2-406f-a9e7-C73F746E9DE','date':'1430341892'写入csv,并且它能够这样做,但我想不出它在第一个脚本中不将这条添加到csv的任何其他原因。我的理由是:它设法打印出*添加的帐户:{'amount':2.3518644466467972e-08,'type':'SELL','price':1396.41,'id':'879aac41-0cc2-406f-a9e7-C73F746E9DE','date':'1430341892'}
因此:
a) writer.writerow(新_帐户)必须已执行…因为它位于同一个“if”块中
b) 它是一个有效的Dict对象。是否对文件调用
close
(显式调用还是使用with
语句调用)?在查看文件之前,程序是否已经通过了close
。。。因此,我认为您在代码前面还遇到了其他问题。请将您的程序缩减为尽可能短的完整程序,以演示错误。有关更多信息,请参阅。