Python - file.write在完成之前中断循环

时间:2018-04-05 19:32:53

标签: python file

def exportOrders(self):

    file = open("orders.txt", 'w')
    file.write("\"Date\"  \"Pair\" \"Amount bought/sold\" \"Pair Price\" \"Profit/Loss\" \"Order Type\"" + '\n')
    for x in self.tradeHistory:

        date = x['date']
        pair = self.currentPair
        amount = x[self.currentPair]
        price = x['price']
        order = x['Order Type']

        if order == "buy":
            spent = x['spent']

            file.write(date + ' ' + pair + ' ' + amount + ' '
                       + price + ' ' + float(-spent) + ' ' + order + ' \n')
        if order == "sell":
            obtained = x['obtained']

            file.write(date + ' ' + pair + ' ' + amount + ' '
                       + price + ' ' + obtained + ' ' + order + ' \n')

    file.close()

self.tradeHistory是一个字典列表,用于存储日期,货币对,购买金额,货币对价格,花费或获得的金额以及订单类型。

我的问题是当程序第一次运行时:

if order == "buy":
    spent = x['spent']

    file.write(date + ' ' + pair + ' ' + amount + ' '
              + price + ' ' + str(float(-spent)) + ' ' + order + ' \n')

for循环中断,orders.txt只显示第一行:

file.write("\"Date\"  \"Pair\" \"Amount bought/sold\" \"Pair Price\" \"Profit/Loss\" \"Order Type\"" + '\n')

提前谢谢!

编辑:

基本上,我的self.tradeHistory具有以下内容

{'date': 1505161800, 'BTC_ETH': 0.7091196761422075, 'price': 0.07050996, 'spent': 0.05, 'Order Type': 'buy'}
{'date': 1505167200, 'BTC_ETH': 0.7091196761422075, 'price': 0.07079909, 'obtained': 0.050205027771963, 'Order Type': 'sell'}
{'date': 1505236500, 'BTC_ETH': 0.7032346826344071, 'price': 0.07110002, 'spent': 0.05, 'Order Type': 'buy'}
{'date': 1505251800, 'BTC_ETH': 0.7032346826344071, 'price': 0.0707705, 'obtained': 0.04976827010737831, 'Order Type': 'sell'}
{'date': 1505680200, 'BTC_ETH': 0.715374411944349, 'price': 0.06989347, 'spent': 0.05, 'Order Type': 'buy'}
{'date': 1505699100, 'BTC_ETH': 0.715374411944349, 'price': 0.071989, 'obtained': 0.05149908854146174, 'Order Type': 'sell'}
{'date': 1505733300, 'BTC_ETH': 0.6879187705515734, 'price': 0.072683, 'spent': 0.05, 'Order Type': 'buy'}
{'date': 1505745000, 'BTC_ETH': 0.6889021311187427, 'price': 0.07257925, 'spent': 0.05, 'Order Type': 'buy'}
{'date': 1505756700, 'BTC_ETH': 1.3768209016703161, 'price': 0.0732, 'obtained': 0.10078329000226714, 'Order Type': 'sell'}
...

词典列表中有63个项目。我的目标是创建一个看起来像

的.txt文件
"Date"  "Pair" "Amount bought/sold" "Pair Price" "Profit/Loss" "Order Type"
1505161800 BTC_ETH 0.7091196761422075 0.07050996 0.05 buy
1505167200 BTC_ETH 0.7091196761422075 0.07079909 0.05 sell
...

1 个答案:

答案 0 :(得分:0)

你不应该用Python中的字符串连接数字。请改用str.format

file.write(
    '{} {} {} {} {} {}\n'
    .format(date, pair, amount, price, float(-spent), order)
)

您还可以使用csv模块来实现更好的实施。

import csv

def exportOrders(self):

    with open("orders.txt", 'w') as file:
        writer = csv.writer(file, delimiter=' ', quotechar='"')
        writer.writerow([
            'Date', 'Pair', 'Amount bought/sold', 'Pair Price',
            'Profit/Loss', 'Order Type'])
        for x in self.tradeHistory:
            date = x['date']
            pair = self.currentPair
            amount = x[self.currentPair]
            price = x['price']
            order = x['Order Type']

            if order == "buy":
                spent = x['spent']
                writer.writerow([
                    date, pair, amount, price,
                    float(-spent), order])
            if order == "sell":
                obtained = x['obtained']
                writer.writerow([
                    date, pair, amount, price,
                    obtained, order])