- 1

Edit JS code with Python

I am going to edit a code in JavaScript with Python using a template, but the code dont work, I do a example for append here. I have two documents: "Test.txt" and "Test.py" at same folder. The content "Test.txt" is: Prueba = new Trabajador("Sujeto de Prueba", 15, 1000, "7069"); And the Python code is: Prueba = open("Test.txt","r") Plantilla = Prueba.read() Prueba.close() print(Plantilla) Plantilla.replace("(\"Sujeto de Prueba\", 15, 1000, \"7069\");","(\"Carolina\", 20, 1500, \"0000\");") print("/////////////////////////////////////////////////////////////////////////////////////////////////") print(Plantilla) but when i execute the code the exit is this: Prueba = new Trabajador("Sujeto de Prueba", 15, 1000, "7069"); ///////////////////////////////////////////////////////////////////////////////////////////////// Prueba = new Trabajador("Sujeto de Prueba", 15, 1000, "7069"); What am I doing wrong? PD: Sorry for my english, i am learning.

3rd Jan 2018, 9:52 PM
Emmanuel David Angarita Quintanilla
Emmanuel David Angarita Quintanilla - avatar
1 Answer
0
you have read the text file contents into memory and tried a replace operation but you have not written it back to the text file. # open the file and read the data with open("Test.txt","r") as my_file: temp = my_file.read() # make the replacement new_text = temp.replace('this', 'that') print(new_text) # write to the file to effect the update of contents with open("Test.txt","w") as my_file: my_file.write(new_text)
4th Jan 2018, 1:36 AM
richard