如何将epoch时间转换为字符串?



我有一个需要保存为csv文件的数据数组10000*3。下面是我的伪代码,但我不知道如何只用numpy(不是pandas)实现它。有人知道怎么做吗?

import numpy as np
import time 
arr = np.random.randn(10000, 3)
# That need  arr[0,0] = time.time(), arr[i, 0] = time.time()  + i
arr[:,0] = time.time() + 1
# And after that, I need to  column 1 to datatime string(like "2021-02-12 12:12:12")
arr[:,0] need to apply `time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(item))` this function.
# After that, the data type is, column1:str, column2:float, column3:float and to save it as a csv file.

这可以通过import csv(预先安装python默认包)完成。参见本教程中的示例:https://www.pythontutorial.net/python-basics/python-write-csv-file/

with open('countries.csv', 'w', encoding='UTF8') as f:
writer = csv.writer(f)
# write the header
writer.writerow(header)
# write the data
for row in arr:
writer.writerow(row)

最新更新