Parsing Text file

Joshua Landau joshua.landau.ws at gmail.com
Tue Jul 2 16:28:26 EDT 2013


On 2 July 2013 20:50, Tobiah <toby at tobiah.org> wrote:
> How do we know whether we have Sometext?
> If it's really just a literal 'Sometext', then
> just print that when you hit maskit.
>
> Otherwise:
>
>
> for line in open('file.txt').readlines():
>
>         if is_sometext(line):
>                 memory = line
>
>         if line == 'maskit':
>                 print memory

My understanding of the question follows more like:

# Python 3, UNTESTED

memory = []
for line in open('file.txt').readlines():
    if line == 'maskit':
        print(*memory, sep="")

    elif line:
        memory.append(line)

    else:
        memory = []



More information about the Python-list mailing list