开发者社区> 问答> 正文

python中的日志文件

您好,有人可以帮我吗,我有一个Python程序,我想把输出的数字注册到一个“日志”中,但是我想把所有的数字都写进去,因为我在程序中使用了open,然后,我使用了+ =,希望na horade写在文件中,程序跳过了一行写在下一行,但是程序出错了,有人能帮忙吗():

hora = str (datetime.now ()). replace (":", "."). split ()
hour = hour [0] + "," + hour [1]
   
name = "log" + time
    
log = open (". txt", "wb")
log + = "," + str (num)

这是日志的一部分

问题来源:stackoverflow

展开
收起
1540413669110688 2020-03-22 18:56:10 758 0
1 条回答
写回答
取消 提交回答
  • 您犯了多个错误。 hour = hour [0] + "," + hour [1] 会给你一个错误,因为应该是hora而不是hour。正确的行:

    hour = hora [0] + "," + hora [1]
    

    第二个错误:

    log = open (". txt", "wb")
    

    在编写文本时,应使用文本模式(“ wt”),而不是二进制(“ wb”)。

    log = open (". txt", "wt")
    

    第三个错误: 写入文件使用file.write()
    所以

    log + = "," + str (num)
    

    应该

    jlog.append("," + str (num))
    

    回答来源:stackoverflow

    2020-03-22 19:07:00
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载