不会把dict行写到csv

时间:2015-04-30 02:47:21

标签: 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}
                            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'} ***

当我只检查我的csv文件时

{'amount': '0.00010000055698286583', 'type': 'BUY', 'price': 1394.95, 'id': "<class 'str'>", 'date': '1430341136'}

在那里......

这很奇怪,因为,我写了一个简单的测试脚本来写{&#39;数量&#39;:2.3518644466467972e-08,&#39;输入&#39;:&#39;卖出&#39;, &#39;价格&#39;:1396.41,&#39; id&#39;:&#39; 879aac41-0cc2-406f-a9e7-c73f7476e9de&#39;,&#39; date&#39;:&#39; 1430341892&#39;}到csv,它能够这样做,但我无法想到为什么它不能在第一个脚本中将这个Dict添加到csv的任何其他原因。我的理由是:它设法打印出 *已添加的帐户:{&#39;金额&#39;:2.3518644466467972e-08,&#39;输入&#39;:&#39;卖出&#39;, &#39;价格&#39;:1396.41,&#39; id&#39;:&#39; 879aac41-0cc2-406f-a9e7-c73f7476e9de&#39;,&#39; date&#39;:&#39; 1430341892&#39;} * 由此得出:

a)writer.writerow(new_account)必须已被执行....因为它是相同的&#39;如果&#39;块。

b)它是一个有效的Dict对象。

0 个答案:

没有答案