read() file??

Bengt Richter bokr at oz.net
Mon Sep 16 15:50:53 EDT 2002


On 11 Sep 2002 18:27:48 +0300, Erno Kuusela <erno-news at erno.iki.fi> wrote:

>In article <mailman.1031684894.10361.python-list at python.org>,
>jubafre at brturbo.com writes:
>
>| I have a file with assembler instruction and i want read the lines
>| of the file and put in a list of string, this is easy to do, but i
>| don´t want to consider the caracteres "/" and the othes after him.
>
>| file.asm:
>| ADD D1  /coment1
>| LDA D2  /coment2
>
>try something like...
>
>def read_asm(filename):
>    out = []
>    for line in open(filename).readlines():
>        out.append(line.split('/')[0])
         out.append(line.split('/')[0].rstrip())  # so as to get ['ADD D1', ...] not ['ADD D1  ', ...]
                                      
>    return out
>
A nit ;-)

Alternatively, if you have 2.2,

 >>> [x.split('/',1)[0].rstrip() for x in file('file.asm')]
 ['ADD D1', 'LDA D2']


where the file is
 >>> file('file.asm').readlines()
 ['ADD D1  /coment1\n', 'LDA D2  /coment2']

Oops. Well, the missing final \n didn't hurt.

Regards,
Bengt Richter



More information about the Python-list mailing list