Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python每天都创建新的文件来进行编写_Python - Fatal编程技术网

Python每天都创建新的文件来进行编写

Python每天都创建新的文件来进行编写,python,Python,我有一个python程序,它从api读取数据并将其写入eth.txt文件。 我想通过编程每天创建一个日期名为eth_9.11.2018.txt、eth_10.11.2018.txt等的新文件。。。。然后写在上面。 你能帮我吗 import time import threading import urllib import datetime while True: ts = time.time() h = datetime.datetime.fromtimestamp(ts)

我有一个python程序,它从api读取数据并将其写入eth.txt文件。 我想通过编程每天创建一个日期名为eth_9.11.2018.txt、eth_10.11.2018.txt等的新文件。。。。然后写在上面。 你能帮我吗

import time
import threading
import urllib
import datetime


while True:
    ts = time.time()
    h = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S') 
    d = datetime.datetime.fromtimestamp(ts).strftime('%d.%m.%Y')
    # read from API
    ethash = urlopen('https://www.coincalculators.io/api/allcoins.aspx?hashrate=420000000&power=2000&powercost=0.15&difficultytime=0&algorithm=Ethash').read()
    dataethash= json.loads(ethash)
    global coinethash,algoethash,dayUSDethash,dayEUethash
    coinethash = dataethash[0]["name"] 
    algoethash = dataethash[0]["algorithm"] 
    dayUSDethash = dataethash[0]["profitInDayUSD"]
    diffethash = dataethash[0]["currentDifficulty"]
    print ("Difficulty:"),diffethash
    print ("Algoritm:"),algoethash
    print ("Coin:"),coinethash
    dayUSDethash = float(dayUSDethash)
    dayEUethash = dayUSDethash*0.88
    # Write ti file
    profit = open("/home/pi/Skripte/eth.txt", "a")    
    profit.write(str(h))
    profit.write('*')
    profit.write(str(d))
    profit.write('*')
    profit.write(str(algoethash))
    profit.write('*')
    profit.write(str(diffethash))
    profit.write('*')
    profit.write(str(coinethash))
    profit.write('*')
    profit.write(str("%.2f" % dayEUethash))
    profit.write('*')
    profit.write("\n") #new line
    print("OK")
    time.sleep(500)
…顺便说一下。。。 是否可以以更友好、更简短的方式阻止“#写入文件”写入? 它必须在单词之间包含一个字符*,并且必须写在新行中。。。
谢谢你的帮助

您可以动态定义要写入的文件名。 在这种情况下,
eth_{}.txt'.format(d)
会将当天附加到它上面

为了更优雅地写入文件,您可以在一行中用“*”将您的字符串加入列表

with open('eth_{}.txt'.format(d), 'a') as profit:
    profit.write('*'.join([str(x) for x in [h, d, algoethash, diffethash, coinethash, "%.2f" % dayEUethash, '\n']]))

对于#write to file块,可以尝试澄清吗?你试过什么?什么不起作用?听起来像是使用yyyy-mm-dd而不是dd.mm.yyyy的工作几乎肯定是个好主意。请浏览@MikeScotty comment中的链接;除了回顾中的示例外,虽然这可能回答了作者的问题,但它缺少一些解释性词语和/或文档链接。如果没有一些短语,原始代码片段就没有多大帮助。你也会发现这很有帮助。请编辑您的答案。对一个尚不存在的文件使用
'a'
是完全可以接受的。@Reupiey,首先是“eth{}.txt”。格式(d)“很好,谢谢,但第二是写得更优雅,不工作-我觉得括号有问题。。我试着用open('eth{}.txt'.format(d),'a')作为利润:和
profit.write('*').join([str(x)表示[h,d,algoethash,differethash,coinethash,“%.2f”%dayeuthash,“\n']”)中的x)
但是有错误“AttributeError:'NoneType'对象没有属性“join”@McDam是的,对不起,我忘了一个括号!您遇到的新错误是,它试图将“join”应用于profit的结果。write(“*”)如果没有,请改为:
profit.write(“*”.join([h,d,algoethash,differthash,coinethash,“%.2f”%dayeuthash,“\n']])
@Reupiey KING!!!工作完美,谢谢你。。。。。(我以为我是在尝试所有的括号组合,但显然不是,哈哈哈)