Python Forum

Full Version: Space in texte file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
i'm trying to do a save file.
Sometime a space is create between two variables.
How can i do for don't have space ?

My code:

class Perso:
    def __init__(self):
        self.nom = 'xxx'
        self.classe = 'xxx'
        self.lvl = 1
        self.xp = 0
        self.pv = 10
perso = Perso()

def Save() :
    liste = [perso.nom, perso.classe, perso.lvl, perso.xp, perso.pv]
    with open('save.txt', 'wt') as save:
        for i in liste:
            save.write("%s\n" % str(i))
           

def Load():
    with open('save.txt', 'rt') as load_nom:
        load = load_nom.readlines()
        perso.nom = load[0]
        perso.classe = load[1]
        perso.lvl = int(load[2])
        perso.xp = int(load[3])
        perso.pv = int(load[4])
        
The error append in file :
XXX

XXX

1
0
10
Thanks for you return :)
because when you read from file, each line will have new-line at the end.
when you convert to int, this new-line is striped. but it's not stripped from the strings.
so you end-up with 2 new lines.
Thanks for your reply.
I ll go change that. 😊