to write set of values to a file from python

M

muttu2244

hi everybody

i want to write a set of values to a file from python.
For ex:: the fields name will "comp name", "ip addr", "mac addr" etc.
And below all these fields i ll have the values for these fields.

it should look some what like this.

comp name ipaddr macaddr

jdasfhjashd 234.347.23.12
334.12.354.43.232
dfdsfusdaufisa 234.533.45.12 89.234.234.2343
sfdidsfoui 234.45.23.56
89.343.23.45.233

something like thiss.

so which is the best way to do this.
I mean which file format would be the best to choose , to dump all
these values from my python script.

Will it be a ".ini" file format? or ".xls" or ".txt" which will be the
better one.
so that later it will be easy for me to read this file.

thanks in advance for the help

regards
yogi
 
D

Dennis Benzinger

hi everybody

i want to write a set of values to a file from python.
For ex:: the fields name will "comp name", "ip addr", "mac addr" etc.
And below all these fields i ll have the values for these fields.

it should look some what like this.

comp name ipaddr macaddr

jdasfhjashd 234.347.23.12
334.12.354.43.232
dfdsfusdaufisa 234.533.45.12 89.234.234.2343
sfdidsfoui 234.45.23.56
89.343.23.45.233

something like thiss.

so which is the best way to do this.
I mean which file format would be the best to choose , to dump all
these values from my python script.

Will it be a ".ini" file format? or ".xls" or ".txt" which will be the
better one.
so that later it will be easy for me to read this file.

thanks in advance for the help

regards
yogi


I would use a comma separated values (CSV) file.
Have a look at the csv module
<http://www.python.org/doc/2.4.2/lib/module-csv.html>.

Bye,
Dennis
 
M

muttu2244

hi dennis,
thanks for the help, itseems its good way to go.
i tried it, and i liked the idea, but there is one problem with it.

its printing each character in a seperate row where as
i want each string in a seperate column?

so how can i do that?? Any help for this.

thanks and regards
yogi
 
D

Dennis Benzinger

[...]
its printing each character in a seperate row where as
i want each string in a seperate column?

so how can i do that?? Any help for this.
[...]

Could you post your code here? Or explain what you did exactly.

Bye,
Dennis
 
M

muttu2244

I have tried with the following code

import csv
file = open("some.csv","wb")
writer = csv.writer(file)
list = ["CompName", "IpAddr", "MacAddr","OpSys"]
for row in list:
writer.writerow(row)

file.close()

And the result am getting in a "some.csv" file is as below

c o m p N a m e
I p A d d r
M a c A d d r
O p S y s

each charatecr its printing in a different cell of excel sheet(some.csv
file).
so can i print the whole strings like "compName" "ipAddr" etc in a
different cells(columns)

thanks and regards
yogi
 
L

Lawrence Oluyede

Il 2005-12-14 said:
I have tried with the following code

import csv
file = open("some.csv","wb")
writer = csv.writer(file)
list = ["CompName", "IpAddr", "MacAddr","OpSys"]
for row in list:
writer.writerow(row)

file.close()

writerow() wants a sequence, a string is a sequence of chars and hence
it writes a char at time, replace with

writer.writerow([row])
 
M

muttu2244

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.

thanks again
yogi


Lawrence said:
Il 2005-12-14 said:
I have tried with the following code

import csv
file = open("some.csv","wb")
writer = csv.writer(file)
list = ["CompName", "IpAddr", "MacAddr","OpSys"]
for row in list:
writer.writerow(row)

file.close()

writerow() wants a sequence, a string is a sequence of chars and hence
it writes a char at time, replace with

writer.writerow([row])
 
B

Bengt Richter

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: ... 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:
... for line in data.splitlines():
... yield line.split()
...

See if that worked: ...
['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:
Write the same rows-as-item-lists as above:
...
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
 
M

muttu2244

hi
thanks every body for the help.

Now how can check whtr the row am reading is the last row or not??

for example for this below data in csv file

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

file = open ('C:\some.csv','r')
reader = csv.reader(file)
for row in reader:
print row
HERE HOW CAN I CHECK WHTR THIS ROW IS THE LAST ONE IN THE FILE

so that if at all i dint find what am searching for i can write that
information at the last row, after opening the file in a "append" mode.

thanks again for help

regards
yogi
 
S

Steve Holden

hi
thanks every body for the help.

Now how can check whtr the row am reading is the last row or not??

for example for this below data in csv file

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

file = open ('C:\some.csv','r')
reader = csv.reader(file)
for row in reader:
print row
HERE HOW CAN I CHECK WHTR THIS ROW IS THE LAST ONE IN THE FILE
Don't do that!
so that if at all i dint find what am searching for i can write that
information at the last row, after opening the file in a "append" mode.
What you should do is use the "else" clause on the for statement, which
is only executed if the loop terminates normally; something like:

for row in reader:
if row is the one you want:
break
else:
add new row

Of course, this presumes you don;t want to just print out everything
that's in the file.

regards
Steve
 
S

Scott David Daniels

hi
thanks every body for the help.
Now how can check whtr the row am reading is the last row or not??

for example: ...
reader = csv.reader(file)
for row in reader:
print row
HERE HOW CAN I CHECK WHTR THIS ROW IS THE LAST ONE IN THE FILE

so that if at all i dint find what am searching for i can write that
information at the last row, after opening the file in a "append" mode.

What Steve Holden says is right. If it turns out you need to know
ahead of time, use a lagged input to determine whether it is last:

def lagged(source):
'''produce element,islast for elements in source'''
generator = iter(source)
previous = generator.next()
for element in generator:
yield previous, False
yield previous, True

file = open ('C:\some.csv','r')
reader = csv.reader(file)
for row, final in lagged(reader):
print row
if final:
print 'final:', row
else:
print 'leads:', row


--Scott David Daniels
(e-mail address removed)
 
S

Scott David Daniels

Scott said:
def lagged(source):
'''produce element,islast for elements in source'''
generator = iter(source)
previous = generator.next()
for element in generator:
yield previous, False
yield previous, True

Oops:
def lagged(source):
'''produce element,islast for elements in source'''
generator = iter(source)
previous = generator.next()
for element in generator:
yield previous, False
previous = element
yield previous, True
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,052
Latest member
EvaW192252

Latest Threads

Top