-3

Suppose i want my text file contains a list of commands:

1. eat, food
   7am
2. brush, teeth
   8am
3. crack, eggs
   1pm

How can we get:

"eat, food\n7am"
"brush, teeth\n8am"
"crack, eggs\n1pm"

I'm trying to use the classic split() with loops, but so far i haven't figured out how to get rid of the numbers.. Any suggestions?

4

1 回答 1

1

使用regexstr.splitlines

>>> import re
>>> s = """1. eat, food
   7am
2. brush, teeth
   8am
3. crack, eggs
   1pm"""
>>> lis = [re.sub(r'^\d+\.\s*', '', x).strip() for x in s.splitlines()]
>>> it = iter(lis)
>>> for x in it:
    print '{!r}'.format(x + '\n' + next(it))


'eat, food\n7am'
'brush, teeth\n8am'
'crack, eggs\n1pm'
于 2013-10-02T08:36:07.247 回答