我是python的新手,我试图将机器上次启动时间的输出保存为csv格式。它应该是"日期"。在一栏和&;time &;在另一个专栏中。下面是简单的python代码,但它没有工作。你能帮个忙吗?
import psutil,datetime
import json
import csv
psutil.boot_time()
with open(r'path') as f:
data=json.load(f)
b = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
print(b)
with open("uptime.csv", "wb") as f:
writer=csv.writer(f)
您可以使用Date()和time()方法。然后使用writerow()
在csv中编写两个单独的列date = datetime.datetime.fromtimestamp(psutil.boot_time()).date().strftime("%Y-%m-%d")
time = datetime.datetime.fromtimestamp(psutil.boot_time()).time().strftime("%H:%M:%S")
with open("uptime.csv", "w") as f:
writer = csv.writer(f, delimiter=',')
writer.writerow([date, time])