3

The following demonstrates the problem:

import io
import numpy as np

a = np.loadtxt(io.StringIO("val1 val2\nval3 val4"), \
               dtype=np.dtype([("col1", "S10"), ("col2", "S10")]))
print("looks weired: %s"%(a["col1"][0]))
assert(a["col1"][0] == "val1")

I don't understand how I should compare the strings. On my system (numpy 1.6.2, python 3.2.2) the output looks like this:

>>> 
looks weired: b'val1'
Traceback (most recent call last):
  File "D:/..../bug_sample.py", line 7, in <module>
    assert(a["col1"][0] == "val1")
AssertionError
4

1 回答 1

4

这与 - 无关numpy

>>> b"asd" == "asd"
False

在 Python 3bytes中,对象不比较等于strings。所以要么:

  • 比较b"val1"而不是"val1"使类型匹配,
  • 将对象解码bytes为字符串(.decode('utf-8')"val1".
于 2013-11-13T15:03:47.970 回答