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()
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:])
+ 1
or, to avoid doubling memory requirement in case of big files:
ext = file.read()
print(text[:i],text[i:],sep='\n')
0
Ohh thank you very much