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

Laura Creighton lac at openend.se
Fri Nov 27 12:07:49 EST 2015


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



More information about the Tutor mailing list