Parse each line by character location

Lie Lie.1296 at gmail.com
Wed Nov 5 10:44:31 EST 2008


On Nov 5, 2:29 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
[snip]
>         So you have a classic (especially for COBOL and older FORTRAN) fixed
> field record layout, no?
>
>         I presume the entire file is of a single layout? That would mean
> only one splitting format is needed...
>
[snip]
>
>         Note that all fields are still in character format. And has been
> noted, I'm sure, if you try to turn the third field into an integer, you
> may have problems, depending upon how you do the conversion -- leading 0
> implies octal if it were a literal, though it seems int() handles it
> correctly (Python 2.5)

from help(int)
 |  ... If base is zero, the proper base is guessed based on the
 |  string content. ...

int(x) will always convert x in base 10
int(x, 0) will convert based on literal int notation, i.e. the prefix
'0x' is base 16 (hex), prefix '0' is base 8 (octal), everything else
is base 10.
int(x, n) will convert on base n, where 2 <= n <= 36

if you're still in doubt, just pass the base explicitly to the int:
int(x, 10) (superfluous though)



More information about the Python-list mailing list