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

Br. Sayan mokshavivek at gmail.com
Sat Nov 28 23:53:29 EST 2015


It  is exactly what Peter has said. Opened the file with 'U' and it worked
like a charm.

Thank you very much!

On 28 November 2015 at 19:10, Peter Otten <__peter__ at web.de> wrote:

> Br. Sayan wrote:
>
> > 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?
>
> Your sample data uses the "\r" character to separate lines. This has gone
> out of fashion, but Python 3 handles it gracefully.
>
> In Python 2 you have to enable "universal newlines" explicitly with
>
> with open("Manwal.txt", "U") as infile, ...
>    ...
>
> This will recognize "\r", "\r\n", and "\n" as line separators, and
> translate
> all of them to "\n".
>
> _______________________________________________
> 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