Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 18th, 2005, 10:55 PM
artixan@gmail.com
Guest
 
Posts: n/a
Default Writting to specific location in a file

I have a database result set in a Python list. Each row is a tuple.
data = [(a_field,b_field,c_field,d_field,x_field,y_field,z _field)]

I need to loop thru this data and write each row to a text file.

f = open('out.txt','w')
for row in data:
f.writelines(row)


My chanllenge is that each field needs to have a exact specific
position in the output file.
For instance a_field needs to start at position 2 in the line, b_fields
at position 18 and so on, for all the fields on each line.


What is a way of approaching this problem. I was think of maybe
creating a Cheetah template file. Is there a simple way to go?

Can anyone point me to a possible way of doing this?


Thanks a lot,

Arnaldo

  #2  
Old July 18th, 2005, 10:56 PM
Michael Hoffman
Guest
 
Posts: n/a
Default Re: Writting to specific location in a file

artixan@gmail.com wrote:
[color=blue]
> My chanllenge is that each field needs to have a exact specific
> position in the output file.
> For instance a_field needs to start at position 2 in the line, b_fields
> at position 18 and so on, for all the fields on each line.[/color]

You can use string formatting operations:

row = ("a", "b")
print " %-16s%-16s" % row

http://docs.python.org/lib/typesseq-strings.html
--
Michael Hoffman
 

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