【问题标题】:How do I specify dtype str correctly?如何正确指定 dtype str?
【发布时间】: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


【解决方案1】:

由于我无法通过 NumPy 做到这一点,所以我已经转移到 Pandas 并且它对我有用。

我是 Python 新手,所以还在学习,但我的想法是,如果我想要一个包含 Float、String、Integers 的数据表,我最好使用 Pandas 并且如果我有一个主要是 Float 的纯表和整数我最好使用 NumPy。

现在 Pandas 为我工作,所以继续前进。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多