0

help please

Im trying to add a newline like "\n" in a code but i cant,so i wanted to know how can i index a newline in my code. I was thinking like about use append but it did not change nothing. file = open("/usercode/files/books.txt", "r") leer=file.read() lista=leer.split() abreviatura="" salto_linea="\n" i=0 for i in lista: i+=i abreviatura+=i[0] if i==2: #here is where i want to put the part of newline print(abreviatura) file.close()

26th Feb 2021, 11:52 PM
Yami Francø
Yami Francø - avatar
3 Answers
+ 2
if you want to put a new line at index i, you could simply do: text = file.read() print(text[:i]+'\n'+text[i:])
27th Feb 2021, 12:34 AM
visph
visph - avatar
+ 1
or, to avoid doubling memory requirement in case of big files: ext = file.read() print(text[:i],text[i:],sep='\n')
27th Feb 2021, 12:36 AM
visph
visph - avatar
0
Ohh thank you very much
27th Feb 2021, 12:43 AM
Yami Francø
Yami Francø - avatar