Python文件.write完成前断开回路

2024-03-28 13:52:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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

你知道吗自我贸易历史是一个字典列表,其中存储日期、一对、购买金额、一对的价格、花费或获得的钱以及订单类型。你知道吗

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

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

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

for循环中断订单.txt仅显示第一行,即:

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

提前谢谢!你知道吗


编辑:

基本上,我的自我贸易历史具有以下内容

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

Tags: selfdatetypeorderbuypricefilewrite
1条回答
网友
1楼 · 发布于 2024-03-28 13:52:25

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

相关问题 更多 >