StringIO示例不起作用

编程入门 行业动态 更新时间:2024-10-26 18:19:29
本文介绍了StringIO示例不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试了解 numpy.getfromtxt 方法和 io.StringIO 的工作方式. 在官方网站上( https ://docs.scipy/doc/numpy-1.13.0/reference/generation/numpy.genfromtxt.html#numpy.genfromtxt )我找到了一些示例.这是其中之一:

I try to understand how works numpy.getfromtxt method and io.StringIO. On the officical website(docs.scipy/doc/numpy-1.13.0/reference/generated/numpy.genfromtxt.html#numpy.genfromtxt) I found some examples. Here is one of them:

s = StringIO("1,1.3,abcde") data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),('mystring','S5')], delimiter=",")

但是当我在计算机上运行此代码时,我得到:TypeError:必须为str或None,而不是字节

But when I run this code on my computer I get: TypeError: must be str or None, not bytes

请告诉我如何解决?

推荐答案

In [200]: np.__version__ Out[200]: '1.14.0'

该示例对我有用:

In [201]: s = io.StringIO("1,1.3,abcde") In [202]: np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ...: ... ('mystring','S5')], delimiter=",") Out[202]: array((1, 1.3, b'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', 'S5')])

它也适用于字节字符串:

It also works for a byte string:

In [204]: s = io.BytesIO(b"1,1.3,abcde") In [205]: np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ...: ... ('mystring','S5')], delimiter=",") Out[205]: array((1, 1.3, b'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', 'S5')])

genfromtxt可以处理任何能为其输入行的内容,因此我通常直接使用字节串列表(在测试问题时):

genfromtxt works with anything that feeds it lines, so I usually use a list of bytestrings directly (when testing questions):

In [206]: s = [b"1,1.3,abcde"] In [207]: np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ...: ... ('mystring','S5')], delimiter=",") Out[207]: array((1, 1.3, b'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', 'S5')])

或多行

In [208]: s = b"""1,1.3,abcde ...: 4,1.3,two""".splitlines() In [209]: s Out[209]: [b'1,1.3,abcde', b'4,1.3,two'] In [210]: np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ...: ... ('mystring','S5')], delimiter=",") Out[210]: array([(1, 1.3, b'abcde'), (4, 1.3, b'two')], dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', 'S5')])

过去是dtype=None和genfromtxt创建的S字符串.

It used to be that with dtype=None, genfromtxt created S strings.

> genfromtxt()中的NumPy dtype问题,读取以字节串形式输入

使用1.14,我们可以控制默认字符串dtype:

With 1.14, we can control the default string dtype:

In [219]: s = io.StringIO("1,1.3,abcde") In [220]: np.genfromtxt(s, dtype=None, delimiter=",") /usr/local/bin/ipython3:1: VisibleDeprecationWarning: Reading unicode strings without specifying the encoding argument is deprecated. Set the encoding, use None for the system default. #!/usr/bin/python3 Out[220]: array((1, 1.3, b'abcde'), dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', 'S5')]) In [221]: s = io.StringIO("1,1.3,abcde") In [222]: np.genfromtxt(s, dtype=None, delimiter=",",encoding=None) Out[222]: array((1, 1.3, 'abcde'), dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', '<U5')])

docs .scipy/doc/numpy/release.html#encoding-argument-for-text-io-functions

现在,我可以生成带有Py3字符串的示例,而不会产生所有难看的b'string'结果(但要记住,并非每个人都已升级到1.14):

Now I can generate examples with Py3 strings without producing all those ugly b'string' results (but got to remember that not everyone has upgraded to 1.14):

In [223]: s = """1,1.3,abcde ...: 4,1.3,two""".splitlines() In [224]: np.genfromtxt(s, dtype=None, delimiter=",",encoding=None) Out[224]: array([(1, 1.3, 'abcde'), (4, 1.3, 'two')], dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', '<U5')])

更多推荐

StringIO示例不起作用

本文发布于:2023-11-15 07:06:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1592227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   不起作用   StringIO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!