to write set of values to a file from python

Bengt Richter bokr at oz.net
Wed Dec 14 23:14:08 EST 2005


On 14 Dec 2005 15:39:02 -0800, muttu2244 at yahoo.com wrote:

>thanks lawrence
>it did work.
>and i have one more question for you
>
>its printing the feilds name in the following way
>
>CompName
>IpAddr
>MacAddr
>
>that means each row its printing one field name, can i make it to print
>each one of the field in different columns, in this way
>
>CompName                   IpAddr                  MacAddr
>
>and then to fill up the values for these fields , something like this
>
>
>CompName               IpAddr                   MacAddr
>XXX                       192.178.23.11            78.23.34.23.12
>YYY                      192.189.22.11           89.23.43.12.34
>ZZZ                        192.179.24.45           89.23.34.12.45
>
>etc.

Ok, we'll copy your data:
 >>> data = """\
 ... CompName               IpAddr                   MacAddr
 ... XXX                       192.178.23.11            78.23.34.23.12
 ... YYY                      192.189.22.11           89.23.43.12.34
 ... ZZZ                        192.179.24.45           89.23.34.12.45
 ... """

and define a generator that will deliver the test data above as a rows in the form
of lists of items, to simulate your source of rows:

 >>> def rowsource():
 ...     for line in data.splitlines():
 ...         yield line.split()
 ...

See if that worked:
 >>> for row in rowsource(): print row
 ...
 ['CompName', 'IpAddr', 'MacAddr']
 ['XXX', '192.178.23.11', '78.23.34.23.12']
 ['YYY', '192.189.22.11', '89.23.43.12.34']
 ['ZZZ', '192.179.24.45', '89.23.34.12.45']

Use csv with default options, which will separate with commas.
Make a writer that will output back to the screen instead of a file,
so we can see without having to mess with an actual file:
 >>> import csv
 >>> import sys
 >>> csv_writer = csv.writer(sys.stdout) # or supply file open for writing

Write the same rows-as-item-lists as above:

 >>> for row in rowsource(): csv_writer.writerow(row)
 ...
 CompName,IpAddr,MacAddr
 XXX,192.178.23.11,78.23.34.23.12
 YYY,192.189.22.11,89.23.43.12.34
 ZZZ,192.179.24.45,89.23.34.12.45

Looks like nothing needed quoting, just comma separation.
Try something different:

 >>> csv_writer.writerow(['has space', 2.5, "has,comma", 'extra','fields','...'])
 has space,2.5,"has,comma",extra,fields,...

Only the comma needed special treatment.

 >>> csv_writer.writerow(['empties:', None, '',[], (), {}, False])
 empties:,,,[],(),{},False

Looks like None and '' translate to the same empty fields (nothing between delimiting commas)
but the other stuff is getting converted by str or repr (we'll check which below)

 >>> csv_writer.writerow(['empties:', None, '','(one comma for each separated field)'])
 empties:,,,(one comma for each separated field)

 >>> csv_writer.writerow([.1])
 0.1
 >>> .1
 0.10000000000000001

Apparently it's str, not repr being used, so you may want to convert to string representation
yourself, according to need. See also help(csv) for other options.

HTH

Regards,
Bengt Richter



More information about the Python-list mailing list