简体   繁体   中英

Python - file.write break loop before finishing

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 is a list of dictionaries that store a date, a pair, the amount bought, the price of the pair, the money spent or obtained, and the order type.

My problem is that when the program runs for the first time into:

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

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

The for loop breaks out and the orders.txt only shows the first line which is:

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

Thank you in advance!


edit:

Basically, my self.tradeHistory has the following content

{'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'}
...

There are 63 items inside the list of dictionaries. My aim is to create a .txt file that looks like

"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
...

You should not concatenate numbers with strings in Python. Use str.format instead:

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

You can also use csv module for a better implementation.

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM