Home > Back-end >  Removing '\n' from a string without using .translate, .replace or strip()
Removing '\n' from a string without using .translate, .replace or strip()

Time:01-21

I'm making a simple text-based game as a learning project. I'm trying to add a feature where the user can input 'save' and their stats will be written onto a txt file named 'save.txt' so that after the program has been stopped, the player can then upload their previous stats and play from where they left off.

Here is the code for the saving:

user inputs 'save' and class attributes are saved onto the text file as text, one line at a time

elif first_step == 'save':
    f = open("save.txt", "w")
    f.write(f'''{player1.name}
    {player1.char_type} #value is 'Wizard'
    {player1.life} 
    {player1.energy}
    {player1.strength}
    {player1.money}
    {player1.weapon_lvl}
    {player1.wakefulness}
    {player1.days_left}
    {player1.battle_count}''')
    f.close()

But, I also need the user to be able to load their saved stats next time they run the game. So they would enter 'load' and their stats will be updated.

I'm trying to read the text file one line at a time and then the value of that line would become the value of the relevant class attribute in order, one at a time. If I do this without converting it first to a string I get issues, such as some lines being skipped as python is reading 2 lines as one and putting them altogether as a list.

So, I tried the following:

In the below example, I'm only showing the data from the class attributes 'player1.name' and 'player1.char_type' as seen above as to not make this question as short as possible.

elif first_step == 'load':
    f = open("save.txt", 'r')        
    player1.name_saved = f.readline() #reads the first line of the text file and assigns it's value to player1.name_saved
    player1.name_saved2 = str(player1.name_saved)  # converts the value of player1.name_saved to a string and saves that string in player1.name_saved2
    player1.name = player1.name_saved2 #assigns the value of player1.name_saved to the class attribute player1.name

    player1.char_type_saved = f.readlines(1) #reads the second line of the txt file and saves it in player1.char_type_saved
    player1.char_type_saved2 = str(player1.char_type_saved) #converts the value of player1.char_type_saved into a string and assigns that value to player1.char_type_saved2

At this point, I would assign the value of player1.char_type_saved2 to the class attribute player1.char_type so that the value of player1.char_type enables the player to load the previous character type from the last time they played the game. This should make the value of player1.char_type = 'Wizard' but I'm getting '['Wizard\n']'

I tried the following to remove the brackets and \n:

final_player1.char_type = player1.char_type_saved2.translate({ord(c): None for c in "[']\n" }) #this is intended to remove everything from the string except for Wizard

For some reason, the above only removes the square brackets and punctuation marks but not \n from the end.

I then tried the following to remove \n:

final_player1.char_type = final_player1.char_type.replace("\n", "")

final_player1.char_type is still 'Wizard\n'

I've also tried using strip() but I've been unsuccessful.

If anyone could help me with this I would greatly appreciate it. Sorry if I have overcomplicated this question but it's hard to articulate it without lots of info. Let me know if this is too much or if more info is needed to answer.

CodePudding user response:

If '\n' is always at the end it may be best to use:

s = 'wizard\n'
s = s[:-1]
print(s, s)

Output:

wizard wizard

But I still think strip() is best:

s = 'wizard\n'
s = s.strip()
print(s, s)

Output:

wizard wizard

CodePudding user response:

Normaly it should work with just

char_type = "Wizard\n"
char_type.replace("\n", "")
print(char_type)

The output will be "Wizard"

  •  Tags:  
  • Related