“AttributeError: 'list' 对象没有属性 'replace'

如何解决“AttributeError: 'list' 对象没有属性 'replace'

我正在尝试替换 txt 文件中某一行的特定部分,但显示“AttributeError: 'list' 对象没有属性 'replace'”。

这是我的代码的一部分:

with open("credentials.txt",'r+') as f:
  credentials_array = f.readlines() # credentials_array contains the txt file's contents,arranged line by line. so credentials_array[0] would be the first login info in the file
  lines_in_credentials = len(credentials_array) # if there are 7 credentials in the text file,lines_in_credentials = 7.

  while x < lines_in_credentials:

    if user in credentials_array[x]: # go through each line in turn to see if the login_username is in one. line 1,credentials_array[1].
      credentials_desired_line = credentials_array[x]

      username_password_score = credentials_array[x].split(",") # username_password_score is the contents of each line,the contents are split by commas
      stored_username = username_password_score[0] # username is part 1
      stored_password = username_password_score[1] # password is part 2
      stored_score = username_password_score[2] # score is part 3
      stored_score_int = int(stored_score)

      if user == stored_username:

        if new_score > stored_score_int:
          print("Congratulations! New high score!")
          print(stored_score_int,"-->",new_score)
          credentials_array_updated = stored_username+","+stored_password+","+str(new_score) # reassign the array[x] to having new_score at the end instead of stored_score
          credentials_array.replace(credentials_array[x],credentials_array_updated)

        break

还有其他方法吗?

解决方法

您在提出的问题中遗漏了一行设置 x = 0,但这并不重要 - 我认为这只是您在写出来时遗漏的一个错字。

您的线路:

credentials_array.replace(credentials_array[x],credentials_array_updated)

是你的问题。试试:

credentials_array[x].replace(credentials_array[x],credentials_array_updated)

replace 对字符串进行操作,您希望替换credentials_array[x] 中的字符串,而不是整个列表。

现在,我假设credentials_desired_line 的条目比您在username_password_score 中概述的条目多。否则,您可以直接进行替换,例如:

credentials_array[x] = credentials_array_updated

作为一个更大的改变,你可以试试这个:

iLines = 0
with open("credentials.txt",'r+') as f:
    credentials_array = f.readlines()  


    for line in credentials_array:
        if user in line: #user we want is in this line

            currScore = int(credentials_array[x].split(",")[2])

            if new_score > currScore:
                print("Congratulations! New high score!")
                print(Str(currScore),"-->",str(new_score))
                credentials_array[iLines].replace(str(currScore),str(newScore))
            break
        iLines =+1
   

如果你想更新文本文件,你的代码的最小修改就是把它放在前面的“with open()”循环的末尾(超出/之外):

with open('credentials.txt','w') as f:
    for line in credentials_array:
        f.write("%s\n" % line)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?