【发布时间】:2017-06-30 05:24:01
【问题描述】:
文件是utility.dat
Name D L J H E M RF AF
line1 150 4.5 2.0 150 2 Copper 0.8 true
line2 140 4.5 2.0 140 2 Aluminium 0.8 true
我的脚本是 script.py
import numpy
configName = "utility.dat"
lines = numpy.genfromtxt(configName,
skip_header=1,
dtype=(str, float, float, float, float, float, str, float, bool))
print(lines[0])
我的结果是
('', 150., 4.5, 2., 150., 2., '', 0.8, True)
Process finished with exit code 0
现在如何正确指定 dtype?因为我不想给出 "S12" 之类的尺寸,因为我无法知道它可以有多大。我也不想使用 dtype=None。
还有其他解决方案吗?
【问题讨论】:
-
所以您知道一种解决方案,即使您不想使用它。另一个是
dtype=None。 -
我也不想用
dtype=None有没有其他办法? -
编写你自己的 csv 加载器。
-
初步加载带有
usecols的字符串列会告诉您正确的S长度。
标签: python-3.x numpy genfromtxt