创建流以在Python中从字符串迭代 [英] creating stream to iterate over from string in Python

查看:156
本文介绍了创建流以在Python中从字符串迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Python中的字符串创建一个流,这样就相当于读取字符串,就像从文本文件中读取一样。类似于:

I want to create a stream from a string in Python so that it's equivalent to reading the string as if it's read from a text file. something like:

for line in open('myfile.txt'): print line

除了'myfile.txt'的内容存储在字符串 s 中。这是正确/最好的方法吗?

except the contents of 'myfile.txt' are stored in a string s. Is this the correct/best way to do it?

s = StringIO.StringIO("a\t\b\nc\td\n")
for line in s: print line


推荐答案


我想用Python中的字符串创建一个流,这样就相当于读取字符串,就像从文本文件中读取一样。

I want to create a stream from a string in Python so that it's equivalent to reading the string as if it's read from a text file.

是的,除非你真的这样做希望它在一个列表中。

Yes, unless you really do want it in a list.

如果打算逐行消费,你的方式是有意义的。

If it is intended to be consumed line by line, the way you are doing it makes sense.

StringIO()创建一个类似文件的对象。

StringIO() creates a file-like object.

文件对象有一个方法, .readlines(),它将对象表示为一个列表。而不是实现列表中的数据,你可以迭代它,这是更多的内存光:

File objects have a method, .readlines(), which materialize the object as a list. Instead of materializing the data in a list, you can iterate over it, which is more memory light:

# from StringIO import StringIO # Python 2 import
from io import StringIO # Python 3 import

txt = "foo\nbar\nbaz"

这里我们将每行附加到列表中,以便我们可以演示迭代文件类对象并保持数据句柄。 (效率更高的是 list(file_like_io)

Here we append each line into a list, so that we can demonstrate iterating over the file-like object and keeping a handle on the data. (More efficient would be list(file_like_io).

m_1 = []
file_like_io = StringIO(txt)
for line in file_like_io:
    m_1.append(line)

现在:

>>> m_1
['foo\n', 'bar\n', 'baz']

你可以退回你的io使用搜索任何索引点

you can return your io to any index point with seek:

>>> file_like_io.seek(0)
>>> file_like_io.tell() #print where we are in the object now
0



如果你真的想要它在列表中



.readlines()具体化了 StringIO 迭代器,好像有人做了 list(io) - 这被认为不太可取。

If you really want it in a list

.readlines() materializes the StringIO iterator as if one did list(io) - this is considered less preferable.

>>> m_2 = file_like_io.readlines() 

我们可以看到我们的结果是一样的:

And we can see that our results are the same:

>>> m_1==m_2
True

请记住,它是在换行符后拆分,保留在换行符中文本也是如此,因此您将为每个打印行获得两个换行符,打印时为双倍行距。

Keep in mind that it is splitting after the newlines, preserving them in the text as well, so you'll get two newlines for every printed line, double-spacing on print.

这篇关于创建流以在Python中从字符串迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆