在python的字典列表中将UNIX时间戳转换为UTC时间



如何将UNIX时间戳转换为UTC时间戳并在python中使用UTC时间戳导出数据到csv ?下面是我的代码:

from csv import DictWriter
orders = client.get_my_sales(symbol='AI95')
with open ('testcsv.csv', 'w') as outfile:
writer = DictWriter (outfile, ('time', 'symbol','orderId','price','qty'))
writer.writeheader()
writer.writerows (orders)

下面是print(client.get_my_sales(symbol='AI95'))

的输出
[{'orderId': 22153,   'price': '500.30000000',   'qty': '8.05000000','qty': '1910.26500000',   'symbol': 'AI95',   'time': 1657118024716},
{'orderId': 22153,   'price': '600.30000000',   'qty': '10.05000000', 'qty': '2040.26500000',   'symbol': 'AI95',   'time': 1657118030000}]

你可以使用这个函数:

def convert_to_utc_from_unix(unix_time):
return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(unix_time))

在python中将UNIX时间戳转换为UTC。它以UTC格式返回时间,像这样:2022-07-06 17:42:14,你可以改变格式,但你喜欢,例如,而不是-,你可以使用/。只要记住import time

然后在转换为UTC后,获取数组并将UNIX时间替换为UTC并将其写入.csv文件。

最新更新