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



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.

.



Relevant Pages

  • Re: Parsing / " characters
    ... have the rest of the code to append the ... - JCL inserts that string at the start of every program, ... >>the code associated with that program - in exact order, ... >>Dim db As Database, tdf As TableDef ...
    (microsoft.public.access.externaldata)
  • Re: Parsing / " characters
    ... create a new record in table TTT containing the string ... >I can append ALL the records from the ... >Dim db As Database, tdf As TableDef ... >> Dim lngFN as Long ...
    (microsoft.public.access.externaldata)
  • Re: [PATCH] [PNP] modalias sysfs export
    ... let the device table match one of these id's in that single string: ... We may loose the ability to support new buses ... schemes without depreciating existing kernel interfaces or parsing strings ...
    (Linux-Kernel)
  • Re: Fastest way to append text to a TextBox
    ... What's the fastest way to append text to a TextBox? ... CPU and, even if I limit the TextBox to just 30KB of text, it takes over ... Private Sub AppendText(ByVal sText As String) ... Dim iLen As Integer = .Text.Length + sText.Length ...
    (microsoft.public.dotnet.languages.vb)
  • RE: SaveWorkbookAsToday()
    ... Create a string variables for the day and convert your date to the format you ... Then append or concatenate the string date where necessary in the file name. ... Dim DateFormat As String ...
    (microsoft.public.excel.programming)