splitting tables

robsom no.mail at no.mail.it
Sun Feb 8 11:58:25 EST 2004


Il Sat, 07 Feb 2004 21:56:23 +0000, Bengt Richter ha scritto:


>  >>> s = """\
>  ... CGA 1990 08 15 13 16 G500-105 D   524.45 J.. R1 1993 01 29 00 00 900069
>  ... CGA 1990 08 16 01 22          D   508.06 J.. R1 1993 01 27 00 00 900065
>  ... """
>  >>> import re
>  >>> rxo = re.compile(
>  ...     '(...) (....) (..) (..) (..) (..) (........) (.)   '
>  ...     '(......) (...) (..) (....) (..) (..) (..) (..) (......)'
>  ... )
>  >>> import csv
>  >>> import sys
>  >>> writer = csv.writer(sys.stdout)
>  >>> for line in s.splitlines():Apparently my system doesn't have a 
>  >>> writer.writerow(*rxo.findall(line))
>  ...
>  CGA,1990,08,15,13,16,G500-105,D,524.45,J..,R1,1993,01,29,00,00,900069
>  CGA,1990,08,16,01,22,        ,D,508.06,J..,R1,1993,01,27,00,00,900065
> 
> To write the csv lines to a file instead of sys.stdout, substitute
> (untested) file('your_csv_output_file.csv') in place of sys.stdout in
> the above, and get your lines from something like (note chopping off the
> trailing newline)
> 
>     for line in file('your_table_file'):
>         line = line.rstrip('\n')
> 
> instead of
> 
>     for line in s.splitlines()
> 
> If you have possible short lines that create no match, you'll need to
> check for those before unpacking (by using the prefixed *) into
> writer.writerow's arg list.

I'm not sure I completely understand what you are suggesting. Use a sort
of "mask" to extract information from the line and put it into a string?
And the use some function of the csv module (which I don't have) to
manipulate this string and output it?
If I got it correct this would work only if all the elements of the
columns have the same number of characters which is not always the case as
I was saying in my reply to Karl Pflästerer above.
Any other idea? Thanks

R





More information about the Python-list mailing list