-1

I am having trouble simply saving items into a file for later reading. When I save the file, instead of listing the items as single items, it appends the data together as one long string. According to my Google searches, this should not be appending the items.

What am I doing wrong?

Code:

with open('Ped.dta','w+') as p:  
    p.write(str(recnum))  # Add record number to top of file  
        for x in range(recnum):    
            p.write(dte[x])        # Write date  
            p.write(str(stp[x]))   # Write Steps number  
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Dazza
  • 1
  • Why do you think this should not be appending the items? When you call `write` on a file, it just writes whatever string you pass it to the current write position in the file, which is exactly what you're seeing. If you want things separated in some way, you have to explicitly write some appropriate kind of separator. – abarnert Jun 01 '18 at 23:59
  • Meanwhile, what are the types of those objects? Just writing `str(…)` for some arbitrary thing may not get you something that you can read back in any useful way. Sometimes this is fine (e.g., if the thing is an int, you can just do `int(line)` at read time), but if it's something more complicated, like a list of dicts or a numpy array or the like, you will probably want to pick a file format rather than just write stuff and hope you can figure out what to do with it later. – abarnert Jun 02 '18 at 00:02
  • Python's `write` is too low-level; you want to write a CSV file. Look into Python `csv` or [pandas.to_csv](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html). – smci Jun 02 '18 at 02:17
  • Does this answer your question? [How to write list of strings to file, adding newlines?](https://stackoverflow.com/questions/5429064/how-to-write-list-of-strings-to-file-adding-newlines) – Gino Mempin May 22 '22 at 06:21

2 Answers2

1

Since you do not show your data or your output I cannot be sure. But it seems you are trying to use the write method like the print function, but there are important differences.

Most important, write does not follow its written characters with any separator (like space by default for print) or end (like \n by default for print). Therefore there is no space between your data and steps number or between the lines because you did not write them and Python did not add them.

So add those. Try the lines

p.write(dte[x]) # Write date
p.write(' ') # space separator
p.write(str(stp[x])) # Write Steps number
p.write('\n') # line terminator

Note that I do not know the format of your "date" that is written, so you may need to convert that to text before writing it.


Now that I have the time, I'll implement @abarnert's suggestion (in a comment) and show you how to get the advantages of the print function and still write to a file. Just use the file= parameter in Python 3, or in Python 2 after executing the statement

from __future__ import print_function

Using print you can do my four lines above in one line, since print automatically adds the space separator and newline end:

print(dte[x], str(stp[x]), file=p)

This does assume that your date datum dte[x] is to be printed as text.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
  • If you think the problem is that the OP expects write to work with `print`, why not show him how to just use `print` with `file=`? – abarnert Jun 02 '18 at 00:52
  • 1
    @abarnert: I tried to show as little change as possible from the OP's original code. – Rory Daulton Jun 02 '18 at 00:55
0

Try adding a newline ('\n') character at the end of your lines as you see in docs. This should solve the problem of 'listing the items as single items', but the file you create may not be greatly structured nonetheless.

For further of your google searches you may want to check serialization, as well as json and csv formats, covered in python standard library.

You question would have befited if you gave very small example of recnum variable + original f.close() is not necessary as you have a with statement, see here at SO.

Evgeny
  • 4,173
  • 2
  • 19
  • 39