简体   繁体   English

“AttributeError:'list' object 没有属性'replace'

[英]"AttributeError: 'list' object has no attribute 'replace'

I'm trying to replace a specific part of a line in a txt file, but it says "AttributeError: 'list' object has no attribute 'replace'".我正在尝试替换 txt 文件中某一行的特定部分,但它显示“AttributeError:'list' object 没有属性'replace'”。

This is part of my code:这是我的代码的一部分:

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

Is there any other way to do it?还有其他方法吗?

Your missing a line setting x = 0 in your presented problem, but that's not important - I think that's just a typo you've missed when writing it out.您在提出的问题中缺少行设置 x = 0 ,但这并不重要 - 我认为这只是您在写出时遗漏的错字。

Your line:您的线路:

credentials_array.replace(credentials_array[x], credentials_array_updated)

is your problem.是你的问题。 Try:尝试:

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

replace operates on the string, and you want to replace the string within credentials_array[x], not the whole list. replace 对字符串进行操作,并且您要替换 credentials_array[x] 中的字符串,而不是整个列表。

Now, I have assumed there are more entries to credentials_desired_line than what you've outlined in username_password_score.现在,我假设 credentials_desired_line 的条目比您在 username_password_score 中概述的要多。 Otherwise you could do just a straight replacement such as:否则,您可以直接替换,例如:

credentials_array[x] = credentials_array_updated

As a bigger change, you could try this:作为一个更大的改变,你可以试试这个:

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 you wanting to update the text file, the minimal mod to your code would be to put this at the end (beyond/outside) the previous "with open()" loop:如果您想更新文本文件,对代码的最小修改是将其放在前面的“with open()”循环的末尾(超出/外部):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:&#39;list&#39;对象没有属性&#39;replace&#39; - AttributeError: 'list' object has no attribute 'replace' AttributeError: 'list' object 没有属性 'replace' Python - AttributeError: 'list' object has no attribute 'replace' Python AttributeError: &#39;list&#39; 对象没有属性 &#39;replace&#39; &quot;fasttext&quot; - AttributeError: 'list' object has no attribute 'replace' "fasttext" AttributeError:“列表”对象没有属性“替换”, - AttributeError: 'list' object has no attribute 'replace', python AttributeError:“ NoneType”对象在列表中没有属性“ replace” - python AttributeError: 'NoneType' object has no attribute 'replace' in list AttributeError:'list'对象在尝试删除字符时没有属性'replace' - AttributeError: 'list' object has no attribute 'replace' when trying to remove character AttributeError:“ list”对象在尝试删除“ / n”时没有属性“ replace” - AttributeError: 'list' object has no attribute 'replace' while trying to remove '/n' AttributeError: 'list' object 没有属性 'replace' Selenium Python - AttributeError: 'list' object has no attribute 'replace' Selenium Python AttributeError:在更改数组期间“列表”对象没有属性“替换” - AttributeError: 'list' object has no attribute 'replace' during change array AttributeError: 'list' object 没有属性 - AttributeError: 'list' object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM