Working with Huge Text Files

John Machin sjmachin at lexicon.net
Sat Mar 19 22:11:33 EST 2005


cwazir at yahoo.com wrote:
> Lorn Davies wrote:
>
> > if  (fields[8] == 'N' or 'P') and (fields[6] == '0' or '1'):
> > ## This line above doesn't work, can't figure out how to struct?
>
> In Python you would need to phrase that as follows:
>   if  (fields[8] == 'N' or fields[8] == 'P') and (fields[6] == '0'
>   or fields[6] == '1'):
> or alternatively:
>   if  (fields[8] in ['N', 'P']) and (fields[6] in ['0', '1']):
>

and given that the files are huge, a little bit of preprocessing
wouldn't go astray:

initially:

valid_8 = set(['N', 'P'])
valid_6 = set(['0', '1'])

then for each record:

if fields[8] in valid_8 and fields[6] in valid_6:

More meaningful names wouldn't go astray either :-)




More information about the Python-list mailing list