简体   繁体   中英

Appending new lines to a text file, and then retrieving them later Python 3

TLDR; writing to file results in goofy file reading later.

I am attempting to create a grid of numbers in a file and then later call them back.

This is my code:

data = []
if os.path.isfile("./" + fname):
    with open(fname, "r") as f:
        for line in f:
            data.append(line.split(","))
else:
    f = open(fname,"w+")
    for i in range(3):
        f.write("0,0,0")
        data.append(["0","0","0"])
    f.close()
print(data)

Simply put, the above code checks to see if a file named the same as fname exists. If not, it creates a new file, otherwise, it stores the "grid" of values as data .

As expected, when I first run it, it prints [["0","0","0"],["0","0","0"],["0","0","0"]] , and creates a new file XXXXXXXX.txt . What goes wrong is that when you run it again, it prints [['0', '0', '00', '0', '00', '0', '0']] , which is wrong. Upon examining the file, the information stored is 0,0,00,0,00,0,0 , NOT:

0,0,0
0,0,0
0,0,0

If I use f.write("0,0,0\\n") , the file looks fine, but the program prints [['0', '0', '0\\n'], ['0', '0', '0\\n'], ['0', '0', '0']] , which is still wrong.

I am very confused, please help.

To write the rows to file, use:

f.write("0,0,0\n")

While reading from the file, remove the new line char with:

data.append(line.rstrip('\n').split(","))

This

for line in f:

does not remove '\\n' at end of lines.
=> When reading and splitting, your last 0 will have the '\\n' still in it.

This

 f.write("0,0,0")

does not autoappend a '\\n' .
=> If you continue to write into the file it will be placed directly after the 0 , leading to 0,0,00,0,00,0,0 .

Fix:
(includes some code to make it an mvce and fix for f = opne(...) wich is bad:

import os
fname = "test.txt"

data = []
if os.path.isfile("./" + fname):
    with open(fname, "r") as f:
        for line in f:
            data.append(line.rstrip('\n').split(",")) # strip '\n'
else:
    with open(fname,"w+") as f:  # do not use f = open(fname,"w+") - it will not auto-
                                 # magically close your filehandle if errors get thrown
        for i in range(3):
            f.write("0,0,0\n")   # add '\n'

            data.append(["0","0","0"])

print(data) 

I sense your problem is more about the data incorrectly printing (or not printing how you want it to). Thhe reason the \\n in your data list isn't displayed how you want it is because you're not directly printing that index (kind of how __str__ and __repr__ differ, but you can see that later). So you have to access each index individually rather than the entire list, which can be done with a for loop.

for layer1 in data:
    print(layer1)

If you try this, you can see, that this will not work either. This is because the data list is composed of even more lists, rather than strings. If you were to replace each list with a string with \\n , it would work. But you want to have lists in a list. Kind of reminds you of something nested, right? Well you can use a nested for loop (get it... because the list reminds you of something nested, and there's nested for loops...yeah ok). So what you do is just create another for loop.

for layer1 in data:
    for layer2 in layer1: # not for layer2 ib data
        print(layer2)

This will print the 0's and create new lines where needed. However, it will do this without any commas, giving you an output like this:

000
000
000

You can fix this by including commas in the actual string that is being appended to data .

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM