[Tutor] Read lines opening with a particular set of characters from a file

Br. Sayan mokshavivek at gmail.com
Sat Nov 28 06:22:07 EST 2015


I am doing the following :

with open('Manwal.txt') as infile, open('Manwal_req.txt','w') as outfile:
    for line in infile:
        if line.startswith(('R')):
            outfile.write(line)

It is executing without error but returns a blank file. Where is the
problem?

On 27 November 2015 at 22:37, Laura Creighton <lac at openend.se> wrote:

> In a message of Fri, 27 Nov 2015 17:05:21 +0530, "Br. Sayan" writes:
> >How can we read specific lines from a text files using python. File
> >attached.
> >
> >Suppose we want to read lines starting with 'R|1|^^^' and 'O|1|'
> >
> >Should we use:
> >
> >line.startswith(("..", "..", "----"))
>
> that matches lines that start with '..'  or '..' or '----'
> so whatever else is going on, there is no need to repeat.
>
> If that is what is literally found in the file then
> line.startswith(('R|1|^^^','O|1|'))
> ought to match them.  But if that syntax means something else, i.e
> an R, followed by a 1, followed by any 4 characters, then you will
> have to explain what the syntax means.
>
> Laura
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list