I got a .txt file with this list structure:
["saelyth", "somehting here", "Not needed", "2013-08-24 14:14:47"]
["anothername", "whatever 1", "Not needed neither", "2013-08-24 15:12:26"]
["athirdone", "just an example", "of the list structure", "2013-08-24 15:12:51"]
And I need to replace the second value of a specific list only if the value 1 is found in a file. The code I'm trying is this one, but so far that one is just appending data to the file instead of replacing it.
horaactual = datetime.datetime.now()
filename = "datosdeusuario.txt"
leyendo = open(filename, 'r+')
buffer = leyendo.read()
leyendo.close()
fechitaguardaips = str(horaactual)[:19]
escribiendo = open(filename, 'r+')
for line in escribiendo:
retrieved = json.loads(line)
if retrieved[0] == user.name:
retrieveddata1 = "prueba"
mythirdvalue = "not important"
escribiendo.write(json.dumps([user.name, retrieveddata1, mythirdvalue, fechitaguardaips])+"\n")
break
escribiendo.close()
I guess the fail is in the line escribiendo.write. However, I've been googling for two hours and I did get this far, but I don't find a specific call to Replace data instead of writing or appending. How can I fix this problem?
This is what happens and shouldn't :(
["saelyth", "prueba", "", "2013-08-27 13:25:14"]
["saelyth", "prueba", "", "2013-08-27 13:25:32"]
["saelyth", "prueba", "", "2013-08-27 13:26:01"]
I also got troubles understanding what a Break does as I'd like to stop infinite loops from my code (it happened to me yesterday in a different section of the program, but I was also using "For line in X" in JSON Python).