- 1

Hello, please somebody help me how to solve " book titles " problem in ' exception & files ' in python

I don't know how to code it ?

25th Oct 2020, 9:59 PM
Anmol
Anmol - avatar
4 ответов
+ 3
A simpler way with 5 lines of code. A simple explanation, loop through all lines, and if a line has trailing/ending "\n" remove it from the count of the length. https://code.sololearn.com/cdaPdMWWeN3W/?ref=app
15th Dec 2020, 6:41 PM
Lam Wei Li
Lam Wei Li - avatar
+ 1
use file.readlines() and then iterate over it ,checking if each line contains "\n" if yes then replace that with empty string ("") and then print line[0]+str(len(line))
25th Oct 2020, 10:29 PM
Abhay
Abhay - avatar
0
Maybe will be help the repeating of this part. https://code.sololearn.com/cPeBWF4tkD8w/?ref=app
25th Oct 2020, 10:09 PM
JaScript
JaScript - avatar
0
file = open("/usercode/files/books.txt", "r") #your code goes here books=file.readlines() for i in books: if i==books[-1]: value=len(i) else: value=len(i)-1 print(i[0]+str(value)) file.close()
25th Apr 2021, 10:30 AM
A Owadud Bhuiyan
A Owadud Bhuiyan - avatar