Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > why writing list to file puts each item from list on seperate line?

Reply
Thread Tools

why writing list to file puts each item from list on seperate line?

 
 
homepricemaps@gmail.com
Guest
Posts: n/a
 
      12-31-2005
if i use the code below to write a list to a file

list = (food, price, store)
data.append(list)
f = open(r"test.txt", 'a')
f.write ( os.linesep.join( list ) )


it outputs to a file like this

apple
..49
star market

and i want it to do

apple, .49. star market

any ideas

 
Reply With Quote
 
 
 
 
limodou
Guest
Posts: n/a
 
      12-31-2005
30 Dec 2005 20:22:52 -0800, <>:
> if i use the code below to write a list to a file
>
> list = (food, price, store)
> data.append(list)
> f = open(r"test.txt", 'a')
> f.write ( os.linesep.join( list ) )
>
>
> it outputs to a file like this
>
> apple
> .49
> star market
>
> and i want it to do
>
> apple, .49. star market
>
> any ideas
>


my box is windows xp, so :

>>> print repr(os.linesep)

'\r\n'

If you want to seperate them with space, you should:

f.write ( ''.join( list ) )

and list = (food, price, store)
the list is not called "list", but "tuple", a real list should be

aList = [food, price, store]
--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit
 
Reply With Quote
 
 
 
 
homepricemaps@gmail.com
Guest
Posts: n/a
 
      12-31-2005
i want them to be on the same line when they are written to the file.
right now they are written like this:

food
price
store

i want them to be written like this

food price store

how do i do that?

 
Reply With Quote
 
limodou
Guest
Posts: n/a
 
      12-31-2005
30 Dec 2005 20:44:29 -0800, <>:
> i want them to be on the same line when they are written to the file.
> right now they are written like this:
>
> food
> price
> store
>
> i want them to be written like this
>
> food price store
>
> how do i do that?
>


>>> print ' '.join(['food', 'price', 'store'])


os.linesep represents newline.


--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-31-2005
On Fri, 30 Dec 2005 20:22:52 -0800, homepricemaps wrote:

> if i use the code below to write a list to a file
>
> list = (food, price, store)


Why are you shadowing the built in type list? This is bad practice. Sooner
or later you will do this:

list = [1, 2, 3]
something = process(list)
.... lots of code ...
# try to convert to a list
myList = list(something)

and then you'll spend ages trying to work out why list() raises an
exception.



> data.append(list)
> f = open(r"test.txt", 'a')
> f.write ( os.linesep.join( list ) )
>
>
> it outputs to a file like this
>
> apple
> .49
> star market


That's what you told it to do. Walk through the code:

>>> data = []
>>> L = ("apple", "0.49", "market")
>>> data.append(L)
>>> data

[("apple", "0.49", "market")] # a list with one tuple

So far so good. But now watch:

>>> f = open(r"test.txt", 'a')


I hope you aren't opening the file EVERY time you want
to write a single line

>>> f.write(os.linesep.join(L))


Remember what L is: ("apple", "0.49", "market"). You now join that list
(actually a tuple) into a single string: "apple\n0.49\nmarket\n" and write
that string to the file.

What happened to data? It never gets used after you append to it.


> and i want it to do
>
> apple, .49. star market


Then what you want to do is change data to a list of strings rather than
a list of tuples. Before appending to data, you join the tuple ("apple",
"0.49", "market") like so:

data.append(", ".join(L) + "\n") # note newline at the end of each line

Then, after you have appended ALL the lines, you open your file once for
writing, and write data in one go:

f.writelines(data)


Hope this helps.



--
Steven.

 
Reply With Quote
 
homepricemaps@gmail.com
Guest
Posts: n/a
 
      12-31-2005
never mind i figured out what you were saying,. worked like a
charm!!!!!

thanks for your help.

yaffa

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what's the diff between puts y and puts "#{y}" in class_eval Raj Singh Ruby 4 01-29-2008 10:16 PM
Suggestion: swap name of "puts" and "print" and rename "puts" to"put_s" Michael Brooks Ruby 22 03-27-2007 04:57 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Seperate validation for seperate user controls on same page moondaddy ASP .Net 2 10-16-2006 04:03 AM
why writing list to file puts each item from list on seperate line? homepricemaps@gmail.com Python 0 12-31-2005 04:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57