Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Space in texte file
#1
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 :)
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks for your reply.
I ll go change that. 😊
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  saving data from text file to CSV file in python having delimiter as space K11 1 2,397 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  from global space to local space Skaperen 4 2,313 Sep-08-2020, 04:59 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020