【发布时间】:2013-09-29 15:26:23
【问题描述】:
这是来自聊天机器人的代码,其目的是将有关用户的所有信息保存到文件中。只要它只在 1 个房间中,它就可以正常工作,但是如果我想将同一用户的信息保存在 2 个不同的房间中,我遇到了问题。该机器人不仅会更新获取用户和房间的信息,而且会始终创建该用户和该房间的新行。
这越来越烦人了,我真的不想经常破坏这段代码,所以我想知道它失败的地方以及如何在不使用 dicts 的情况下以适当的方式修复它。 (您可以阅读代码中的所有 cmets 以了解我认为它是如何工作的)。 感谢您的宝贵时间。
#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"]
效果很好...但是它不会更新信息。如果现在我在这两个房间中的任何一个上讲话,机器人不会更新正确的行,而是会在文件中添加更多新行,这就是问题所在。
【问题讨论】:
-
是的,这有点像个人随机项目,只是为了学习 python,所以我不认为我只是设置名称只是为了让我理解。现在我看到这是一个问题,因为它们是西班牙语,而且我需要共享代码。 - 下次吸取教训。
-
您应该将您的问题隔离到最多 10 行代码,我盯着您的代码看了 5 分钟,但西班牙语变量让我无法理解。
-
可悲的是,这是我可以隔离它的最低限度。我确定问题出在第二个 Else 之后:这将是最后 15 行。尽管如此,该部分从上面获取信息,这就是我添加它的原因。很抱歉造成混乱:(