Python 正在尝试检查文件中的两个值是否匹配

Python 正在尝试检查文件中的两个值是否匹配,python,json,file,list,Python,Json,File,List,这是来自聊天机器人的代码,其目的是将用户的所有信息保存到文件中。只要它只在一个房间里就行了,但是如果我想在两个不同的房间里保存同一用户的信息,我就遇到了一个问题。机器人不会仅仅更新获取用户和房间的信息,相反,它将始终创建该用户和房间的新行和新行 它越来越烦人,我真的不想经常破坏这段代码,所以我想知道它在哪里失败,以及如何在不使用dicts的情况下以正确的方式修复它。(您可以阅读代码中的所有注释,以了解我认为它是如何工作的)。 谢谢你抽出时间 #First of all it reads the

这是来自聊天机器人的代码,其目的是将用户的所有信息保存到文件中。只要它只在一个房间里就行了,但是如果我想在两个不同的房间里保存同一用户的信息,我就遇到了一个问题。机器人不会仅仅更新获取用户和房间的信息,相反,它将始终创建该用户和房间的新行和新行

它越来越烦人,我真的不想经常破坏这段代码,所以我想知道它在哪里失败,以及如何在不使用dicts的情况下以正确的方式修复它。(您可以阅读代码中的所有注释,以了解我认为它是如何工作的)。 谢谢你抽出时间

#First of all it reads the file
leyendoestadisticas = open("listas\Estadisticas.txt", "r")
bufferestadisticas = leyendoestadisticas.read()
leyendoestadisticas.close()
if not '"'+user.name+'"' in bufferestadisticas: #If the name of the user is not there, it adds all the information.
  escribiendoestadisticas = open("listas\Estadisticas.txt", 'a')
  escribiendoestadisticas.write(json.dumps([user.name, palabrasdelafrase, letrasdelafrase,
                                            "1", user.nameColor, user.fontColor, user.fontFace, user.fontSize,
                                            message.body.replace('"', "'"), room.name, 0, "primermensajitodeesapersona", fixedrooms])+"\n")
  escribiendoestadisticas.close()
else: #If the name it's there, it will do the next:
  #First of all, get all rooms where the name is saved, to do that...
  listadesalas = []
  for line in open("listas\Estadisticas.txt", 'r'):
          retrieved3 = json.loads(line)
          if retrieved3[0] == user.name: #If the name is found
            if not retrieved3[9] == room.name: #But room is diferent
              listadesalas.append(retrieved3[9]) #Adds the room to a temporal list
  #Now that we got a list with all different lines of that user based on rooms... we do the next code
  data = []
  hablaenunanuevasala = "no"
  with open('listas\Estadisticas.txt', 'r+') as f:
    for line in f:
      data_line = json.loads(line)
      if data_line[0] == user.name: #If name is there
        if data_line[9] == room.name: #And the room matches with actual room, then update that line.
          data_line[1] = int(data_line[1])+int(palabrasdelafrase)
          data_line[2] = int(data_line[2])+int(letrasdelafrase)
          data_line[3] = int(data_line[3])+1
          data_line[4] = user.nameColor
          data_line[5] = user.fontColor
          data_line[6] = user.fontFace
          data_line[7] = user.fontSize
          data_line[11] = data_line[8]
          data_line[8] = message.body.replace('"', "'")
          data_line[9] = room.name
          data_line[12] = fixedrooms
        else: #but if the user is there and room NOT matches, we want to add a new line to the file with the same user but a new room.
          if not room.name in listadesalas: #And here is where i believe is the problem of my code.
            hablaenunanuevasala = "si" #needed since i didn't found a way to properly add a new line inside this loop, so must be done outside the loop later.
      data.append(data_line)
    f.seek(0)
    f.writelines(["%s\n" % json.dumps(i) for i in data])
    f.truncate()
    #Outside the loop - This would work if the program noticed it's a room that is not saved yet in the file for that user.
    if hablaenunanuevasala == "si":
      escribiendoestadisticas2 = open("listas\Estadisticas.txt", 'a')
      escribiendoestadisticas2.write(json.dumps([user.name, palabrasdelafrase, letrasdelafrase,
                                                "1", user.nameColor, user.fontColor, user.fontFace, user.fontSize,
                                                message.body.replace('"', "'"), room.name, 0, "primermensajitodeesapersona", fixedrooms])+"\n")
      escribiendoestadisticas2.close()
所以。。。这就是我尝试过的,只要是一个房间,它就会一直更新信息。当我在第二个房间里讲话时,它会为我在第二个房间里添加一张新唱片(完美)。但是,如果我在这两个房间中的任何一个房间再次发言,机器人会向文件中再添加两行代码,而不是更新我发言的房间的信息

编辑让我总结一下: 假设我在“无论何时”的房间里讲话,该文件将保存一条记录

["saelyth", "whenever", "more info"]
["saelyth", "anotherroom", "more info"]
如果我在另一个房间讲话,文件应该保存一个记录

["saelyth", "whenever", "more info"]
["saelyth", "anotherroom", "more info"]
它工作得很好。。。但它不会更新信息。如果现在我在这两个房间中的任何一个房间发言,机器人将不会更新正确的行,而是会向文件中添加更多新行,这就是问题所在。

修复完成。。。以某种方式
我确实选择了将每个房间的信息保存到不同的文件中,这很有效。

是的,这是一个个人随机项目,只是为了学习python,所以我认为如果我只是为了理解而设置名称也没关系。现在我发现这是一个问题,因为它们是西班牙语的,而且我需要共享代码。-下一次的教训。你应该把你的问题隔离到最多10行代码,我盯着你的代码看了5分钟,但西班牙语变量让我无法理解。遗憾的是,这是我能隔离的最小值。我敢肯定问题出在第二次之后,否则:威奇会排到最后15行。尽管如此,那个部分还是从上面得到了信息,这就是为什么我也添加了它。很抱歉造成混淆:(