How-to create a Pascal Record using Python

Steve Holden sholden at holdenweb.com
Thu Jan 4 14:21:27 EST 2001


Daniel <Daniel.Kinnaer at AdValvas.be> wrote in message
news:3a54c7cf.15612195 at news.skynet.be...
> Hello,
>
> How are Pascal-records used in Python? I've tried some code (below)
> but I ran into trouble with this '\012' attached to each line.  Can
> someone please help me out?  What is the correct way of
> using/creating/writing/reading Pascal-records in Python? Thanks
>
> Daniel
>
>
> """
> This is the contents of the records-file (Each line is a record
> containing 3 elements.)
> aaa,12,azert
> bbb,a1,qsdfg
> ccc,2c,wxcvb
> ddd,7f,yuiop
> eee,9b,hjklm
> fff,0e,nhyuj
> """
>
> #define a function which stores all the lines from a file into a list
> def getTheLines(filename):
>     file = open(filename)
>     return map(str, file.readlines())
>
> #store contents of records-file into a list
> lijst = getTheLines( "C:\\Python20\\AProject\\Records\\records.txt")
>
> print lijst
>
> The output is as follows :
> ['aaa,12,azert\012', 'bbb,a1,qsdfg\012', 'ccc,2c,wxcvb\012',
> 'ddd,7f,yuiop\012', 'eee,9b,hjklm\012', 'fff,0e,nhyuj']
>
Fairly obviously, you need to trim the line-feed characters off the lines
returned by the readlines() method.  I'm not sure why you are mapping the
str() function on the lines, since I would expect that they are strings
already.  But since you have a map call in there, why not map the
string.rstrip() function instead?

A more 2.0-ish way, since you're abviously prepared to read the whole file
in all at once, would be to use the splitlines() string method on a read()
of the file.  This would make the last line of your getTheLines() function

    return file.read().splitlines()

Either of these should suit.

regards
 Steve







More information about the Python-list mailing list