Python readline() from a string?

后端 未结 5 1388
予麋鹿
予麋鹿 2020-12-23 20:21

In python, is there a built-in way to do a readline() on string? I have a large chunk of data and want to strip off just the first couple lines w/o doing split() on the who

相关标签:
5条回答
  • 2020-12-23 20:39

    The easiest way for both python 2 and 3 is using string's method splitlines(). This returns a list of lines.

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

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

    0 讨论(0)
  • 2020-12-23 20:48

    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).

    >>> 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))
    
    0 讨论(0)
  • 2020-12-23 20:52

    in Python string have method splitlines

    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)
    
    0 讨论(0)
  • 2020-12-23 20:59

    Python 2

    You can use 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 StringIO
    >>> buf = StringIO.StringIO(msg)
    >>> buf.readline()
    'Bob Smith\n'
    >>> buf.readline()
    'Jane Doe\n'
    

    Be sure to use cStringIO if performance is important.

    Python 3

    You can use io.StringIO:

    >>> import io
    >>> buf = io.StringIO(msg)
    >>> buf.readline()
    'Bob Smith\n'
    >>> buf.readline()
    'Jane Doe\n'
    >>> len(buf.read())
    44
    
    0 讨论(0)
  • 2020-12-23 21:06

    Do it like StringIO does it:

    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...

    0 讨论(0)
提交回复
热议问题