StringIO example does not work

后端 未结 2 1765
Happy的楠姐
Happy的楠姐 2021-01-21 08:37

I try to understand how works numpy.getfromtxt method and io.StringIO. On the officical website(https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/num

2条回答
  •  遥遥无期
    2021-01-21 08:53

    Consider upgrading numpy because for the current version of numpy, your code just works as written. See the mention in 1.14.0 release note highlights and the section Encoding argument for text IO functions for the relevant changes in np.genfromtxt.

    For older numpy, you use a string object for the input but the docs you linked say:

    Note that generators must return byte strings in Python 3k. 
    

    So do what the docs say and give it a byte string:

    import io
    s = io.BytesIO(b"1,1.3,abcde")
    

提交回复
热议问题