运行脚本会导致异常: TypeError int()参数必须是字符串,类似字节的对象或数字,而不是'datetime.datetime'
data = np.asarray(tmp, dtype=data_type)
from datetime import datetime
import numpy as np
data_type = [("Scan", "i4"), ("Time", "M8[us]"), ("101 (C)", "f8"), ("Alarm
101", "?"), ("102 (C)", "f8"), ("Alarm 102", "?")]
data_text = """1,12/6/2018 09:22:25:462,22.661,0,22.046,0
2,12/6/2018 09:22:35:446,22.874,0,22.289,0
3,12/6/2018 09:22:45:446,22.948,0,22.385,0"""
def main():
tmp = []
for line in data_text.split("\n"):
dataLine = []
for (h, _), v in zip(data_type, line.split(",")):
if h == "Scan":
dataLine.append(int(v))
elif h == "Time":
# dataLine.append(datetime.strptime(v, "%m/%d/%Y %H:%M:%S:%f"))
dataLine.append(np.datetime64(datetime.strptime(v, "%m/%d/%Y %H:%M:%S:%f"), "us"))
elif "Alarm" in h:
dataLine.append(bool(v))
else:
dataLine.append(float(v))
tmp.append(dataLine)
data = np.asarray(tmp, dtype=data_type)
if __name__ == "__main__":
main()
从numpy文档中,我看不出它不起作用的原因。我所能想到的最好的事情是,我缺少有关numpy结构化数组的某些知识。是语法问题,numpy错误,还是可能与numpy有关?
我的设置:
答案 0 :(得分:0)
您只需要将dataLine
列表转换为元组,它就可以正常工作。那是因为您的array
应该保留形状(3,)。使用dataLine
作为列表asarray
会将每个单独的值解释为形状为(3,6)的多维数组的一部分,并且您想要的dtype将不受尊重。
def main():
tmp = []
for line in data_text.split("\n"):
dataLine = []
for (h, _), v in zip(data_type, line.split(",")):
if h == "Scan":
dataLine.append(int(v))
elif h == "Time":
# dataLine.append(datetime.strptime(v, "%m/%d/%Y %H:%M:%S:%f"))
dataLine.append(np.datetime64(datetime.strptime(v, "%m/%d/%Y %H:%M:%S:%f"), "us"))
elif "Alarm" in h:
dataLine.append(bool(v))
else:
dataLine.append(float(v))
tmp.append(tuple(dataLine))
data = np.asarray(tmp, dtype=data_type)