简体   繁体   English

Python readline() 来自字符串?

[英]Python readline() from a string?

In python, is there a built-in way to do a readline() on string?在python中,是否有内置的方法来对字符串执行 readline() ? I have a large chunk of data and want to strip off just the first couple lines w/o doing split() on the whole string.我有一大块数据,只想去掉前几行,而不是在整个字符串上执行 split() 。

Hypothetical example:假设示例:

def handleMessage(msg):
   headerTo  = msg.readline()
   headerFrom= msg.readline()
   sendMessage(headerTo,headerFrom,msg)

msg = "Bob Smith\nJane Doe\nJane,\nPlease order more widgets\nThanks,\nBob\n"
handleMessage(msg)

I want this to result in: sendMessage("Bob Smith", "Jane Doe", "Jane,\\nPlease order...")我希望这导致: sendMessage("Bob Smith", "Jane Doe", "Jane,\\nPlease order...")

I know it would be fairly easy to write a class that does this, but I'm looking for something built-in if possible.我知道编写一个这样做的类会相当容易,但如果可能的话,我正在寻找内置的东西。

EDIT: Python v2.7编辑:Python v2.7

In Python 3, you can use io.StringIO :在 Python 3 中,您可以使用io.StringIO

>>> msg = "Bob Smith\nJane Doe\nJane,\nPlease order more widgets\nThanks,\nBob\n"
>>> msg
'Bob Smith\nJane Doe\nJane,\nPlease order more widgets\nThanks,\nBob\n'
>>>
>>> import io
>>> buf = io.StringIO(msg)
>>> buf.readline()
'Bob Smith\n'
>>> buf.readline()
'Jane Doe\n'
>>> len(buf.read())
44

In Python 2, you can use StringIO (orcStringIO if performance is important):在Python 2,你可以使用StringIO的(或cStringIO如果性能是很重要的):

>>> import StringIO
>>> buf = StringIO.StringIO(msg)
>>> buf.readline()
'Bob Smith\n'
>>> buf.readline()
'Jane Doe\n'

The easiest way for both python 2 and 3 is using string's method splitlines (). python 2 和 3 最简单​​的方法是使用字符串的方法splitlines ()。 This returns a list of lines.这将返回一个行列表。

>>> "some\nmultilene\nstring\n".splitlines()

['some', 'multilene', 'string'] ['some', 'multilene', 'string']

Why not just only do as many splits as you need?为什么不只是根据需要进行尽可能多的拆分? Since you're using all of the resulting parts (including the rest of the string), loading it into some other buffer object and then reading it back out again is probably going to be slower, not faster (plus the overhead of function calls).由于您正在使用所有结果部分(包括字符串的其余部分),将其加载到其他缓冲区对象中,然后再次读取它可能会更慢,而不是更快(加上函数调用的开销) .

If you want the first N lines separated out, just do .split("\\n", N) .如果您想将前N行分开,只需执行.split("\\n", N)

>>> foo = "ABC\nDEF\nGHI\nJKL"
>>> foo.split("\n", 1)
['ABC', 'DEF\nGHI\nJKL']
>>> foo.split("\n", 2)
['ABC', 'DEF', 'GHI\nJKL']

So for your function:所以对于你的功能:

def handleMessage(msg):
   headerTo, headerFrom, msg = msg.split("\n", 2)
   sendMessage(headerTo,headerFrom,msg)

or if you really wanted to get fancy:或者如果你真的想变得花哨:

# either...
def handleMessage(msg):
   sendMessage(*msg.split("\n", 2))

# or just...
handleMessage = lambda msg: sendMessage(*msg.split("\n", 2))

Do it like StringIO does it:像 StringIO 那样做:

i = self.buf.find('\n', self.pos)

So this means:所以这意味着:

pos = msg.find("\n")
first_line = msg[:pos]
...

Seems more elegant than using the whole StringIO...似乎比使用整个 StringIO 更优雅...

in Python string have method splitlines在 Python 字符串中有方法分割线

msg = "Bob Smith\nJane Doe\nJane,\nPlease order more widgets\nThanks,\nBob\n"
msg_splitlines = msg.splitlines()
headerTo = msg_splitlines[0]
headerFrom= msg_splitlines[1]
sendMessage(headerTo,headerFrom,msg)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM