Python readline() from a string?

后端 未结 5 1412
予麋鹿
予麋鹿 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 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...

提交回复
热议问题