Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 14th, 2005, 06:45 PM
muttu2244@yahoo.com
Guest
 
Posts: n/a
Default to write set of values to a file from python

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

  #2  
Old December 14th, 2005, 07:35 PM
Dennis Benzinger
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

muttu2244@yahoo.com schrieb:[color=blue]
> 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
>[/color]


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
  #3  
Old December 14th, 2005, 09:25 PM
muttu2244@yahoo.com
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

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

  #4  
Old December 14th, 2005, 09:55 PM
Dennis Benzinger
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

muttu2244@yahoo.com schrieb:[color=blue]
> [...]
> 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.
> [...][/color]

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

Bye,
Dennis
  #5  
Old December 14th, 2005, 10:25 PM
muttu2244@yahoo.com
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

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

  #6  
Old December 14th, 2005, 10:55 PM
Lawrence Oluyede
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

Il 2005-12-14, muttu2244@yahoo.com <muttu2244@yahoo.com> ha scritto:[color=blue]
> 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()[/color]

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

writer.writerow([row])



--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
  #7  
Old December 14th, 2005, 11:45 PM
muttu2244@yahoo.com
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

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 Oluyede wrote:[color=blue]
> Il 2005-12-14, muttu2244@yahoo.com <muttu2244@yahoo.com> ha scritto:[color=green]
> > 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()[/color]
>
> writerow() wants a sequence, a string is a sequence of chars and hence
> it writes a char at time, replace with
>
> writer.writerow([row])
>
>
>
> --
> Lawrence - http://www.oluyede.org/blog
> "Anyone can freely use whatever he wants but the light at the end
> of the tunnel for most of his problems is Python"[/color]

  #8  
Old December 15th, 2005, 04:25 AM
Bengt Richter
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

On 14 Dec 2005 15:39:02 -0800, muttu2244@yahoo.com wrote:
[color=blue]
>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.[/color]

Ok, we'll copy your data:[color=blue][color=green][color=darkred]
>>> data = """\[/color][/color][/color]
... 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:
[color=blue][color=green][color=darkred]
>>> def rowsource():[/color][/color][/color]
... for line in data.splitlines():
... yield line.split()
...

See if that worked:[color=blue][color=green][color=darkred]
>>> for row in rowsource(): print row[/color][/color][/color]
...
['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:[color=blue][color=green][color=darkred]
>>> import csv
>>> import sys
>>> csv_writer = csv.writer(sys.stdout) # or supply file open for writing[/color][/color][/color]

Write the same rows-as-item-lists as above:
[color=blue][color=green][color=darkred]
>>> for row in rowsource(): csv_writer.writerow(row)[/color][/color][/color]
...
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:
[color=blue][color=green][color=darkred]
>>> csv_writer.writerow(['has space', 2.5, "has,comma", 'extra','fields','...'])[/color][/color][/color]
has space,2.5,"has,comma",extra,fields,...

Only the comma needed special treatment.
[color=blue][color=green][color=darkred]
>>> csv_writer.writerow(['empties:', None, '',[], (), {}, False])[/color][/color][/color]
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)
[color=blue][color=green][color=darkred]
>>> csv_writer.writerow(['empties:', None, '','(one comma for each separated field)'])[/color][/color][/color]
empties:,,,(one comma for each separated field)
[color=blue][color=green][color=darkred]
>>> csv_writer.writerow([.1])[/color][/color][/color]
0.1[color=blue][color=green][color=darkred]
>>> .1[/color][/color][/color]
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
  #9  
Old December 15th, 2005, 09:45 PM
muttu2244@yahoo.com
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

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

  #10  
Old December 16th, 2005, 07:25 AM
Steve Holden
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

muttu2244@yahoo.com wrote:[color=blue]
> 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
>[/color]
Don't do that!
[color=blue]
> 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.
>[/color]
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

  #11  
Old December 16th, 2005, 03:35 PM
Scott David Daniels
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

muttu2244@yahoo.com wrote:[color=blue]
> 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.[/color]

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
scott.daniels@acm.org
  #12  
Old December 16th, 2005, 06:15 PM
Scott David Daniels
Guest
 
Posts: n/a
Default Re: to write set of values to a file from python

Scott David Daniels wrote:[color=blue]
>
> 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[/color]

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
[color=blue]
>
> 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
> scott.daniels@acm.org[/color]


--
-Scott David Daniels
scott.daniels@acm.org
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles