【问题标题】:Removing '\n' from a string without using .translate, .replace or strip()在不使用 .translate、.replace 或 strip() 的情况下从字符串中删除 '\n'
【发布时间】:2022-01-20 17:51:53
【问题描述】:

我正在制作一个简单的基于文本的游戏作为学习项目。我正在尝试添加一个功能,用户可以输入“保存”,他们的统计数据将被写入一个名为“save.txt”的 txt 文件,以便在程序停止后,玩家可以上传他们以前的统计数据和从他们停止的地方开始播放。

这是保存的代码:

用户输入“保存”和类属性以文本形式保存到文本文件中,一次一行

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()

但是,我还需要用户能够在下次运行游戏时加载他们保存的统计数据。所以他们会输入“load”,然后他们的统计数据就会更新。

我正在尝试一次读取一行文本文件,然后该行的值将依次成为相关类属性的值,一次一个。如果我在不先将其转换为字符串的情况下执行此操作,则会出现问题,例如某些行被跳过,因为 python 将 2 行作为一个读取并将它们完全作为一个列表。

所以,我尝试了以下方法:

在下面的示例中,我只显示了上面看到的类属性“player1.name”和“player1.char_type”的数据,以免使这个问题尽可能简短。

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

此时,我会将 player1.char_type_saved2 的值分配给类属性 player1.char_type,以便 player1.char_type 的值使玩家能够加载上次玩游戏时的前一个角色类型。这应该使 player1.char_type = 'Wizard' 的值,但我得到 '['Wizard\n']'

我尝试了以下方法来删除括号和\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

由于某种原因,上面只删除了方括号和标点符号,但没有从末尾删除 \n。

然后我尝试了以下方法来删除\n:

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

final_player1.char_type 仍然是 'Wizard\n'

我也尝试过使用 strip(),但没有成功。

如果有人能帮助我解决这个问题,我将不胜感激。抱歉,如果我把这个问题复杂化了,但是如果没有大量信息就很难说清楚。让我知道这是否太多或是否需要更多信息来回答。

【问题讨论】:

  • f.readline() 返回一个字符串。无需致电str
  • strip() 应该可以工作。 player1.name_saved = f.readline().strip()
  • 欢迎来到 Stack Overflow。请阅读How to Ask 并尝试创建minimal reproducible example。 “但如果没有大量信息,很难说清楚” 只是因为您不习惯试图缩小问题范围。例如,您是否可以使用属性较少的Player 类引起问题?是所有属性都出现问题,还是仅部分出现问题?如果它只发生在其中一些,你能看出它们的处理方式有什么不同吗?
  • 我建议你使用JSON或Pickle来保存数据,而不是在你自己的代码中读写文本文件。
  • 提示:#reads the first line of the text file and assigns it's value to player1.name_saved 旁边的代码说明了什么?代码在注释#reads the second line of the txt file and saves it in player1.char_type_saved 旁边说什么?这两行代码是否使用相同的方法来读取文件?他们有理由使用不同的方法吗?提示:当您执行str(['\n']) 时会发生什么?结果字符串中有多少个字符,它们是什么?

标签: python python-3.x string class


【解决方案1】:

如果'\n' 总是在最后,最好使用:

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

输出:

wizard wizard

但我仍然认为strip() 最好:

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

输出:

wizard wizard

【讨论】:

    【解决方案2】:

    通常它应该只使用

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

    输出将是“向导”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 2019-11-20
      • 1970-01-01
      • 2018-06-29
      • 2011-10-20
      相关资源
      最近更新 更多