【问题标题】:How convert epoch time to string?如何将纪元时间转换为字符串?
【发布时间】:2021-09-29 08:32:30
【问题描述】:

我有一个数据数组 10000*3 需要保存为 csv 文件。下面是我的伪代码,但我不知道如何仅使用 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.

【问题讨论】:

    标签: python-3.x numpy


    【解决方案1】:

    这可以通过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)
    

    【讨论】:

    • 效率太低了,我的代码源数据文件是numpy风格的,100个文件40M大小。
    猜你喜欢
    • 1970-01-01
    • 2011-06-21
    • 2013-01-25
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2017-11-10
    • 2013-08-25
    相关资源
    最近更新 更多