0

这是来自聊天机器人的代码,其目的是将有关用户的所有信息保存到文件中。只要它只在 1 个房间里就可以正常工作,但是如果我想将同一用户的信息保存在 2 个不同的房间里,我遇到了问题。机器人不仅会更新获取用户和房间的信息,而且总是会创建该用户和该房间的新行和新行。

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

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

所以...这就是我尝试过的,只要它是 1 个房间,它就可以完美运行,它会一直更新信息。当我在第二个房间讲话时,它为我添加了第二个房间的新记录(完美)。但是,如果我在这 2 个房间中的任何一个再次讲话,机器人将在文件中添加 2 行代码,而不是更新我讲话的房间的信息。

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

["saelyth", "whenever", "more info"]

如果我在另一个房间讲话,文件应该保存记录

["saelyth", "anotherroom", "more info"]

它工作得很好......但它不会更新信息。如果现在我在这两个房间中的任何一个上讲话,机器人不会更新正确的行,而是会在文件中添加更多新行,这就是问题所在。

4

1 回答 1

0

修复完成......不知何故。我确实选择将信息保存到每个房间的不同文件中,这是可行的。

于 2013-10-02T10:04:00.110 回答