+ 5
Python 3 => 4th project (Book titles) [[solved]]
Have doubt why "\n" was included in character count? {Counted in len()} My Code: file = open("/usercode/files/books.txt", "r") for line in file : print(line[0]+str(len(line))) file.close() My Output: Expected Output: H13 H12 T17 T16 P20 P19 G18 G18
22 Answers
+ 26
file = open("/usercode/files/books.txt", "r")
for line in file :
if line[-1] == "\n" :
print(line[0]+str(len(line)-1))
else :
print(line[0]+str(len(line)))
file.close()
Now it's fine and running
+ 9
Here is a one liner to evaluate the solution:
file = open("/usercode/files/books.txt", "r")
#for loop
for line in file:
#one line print statement
print(line[0] + str(len(line.strip())))
file.close()
+ 6
Thanks codemonkey ☺️
+ 6
A bit late, but i think it's worth to have a view on it.
i will introduce a code that reads a txt file, and strips off the "\n" new line sequence. the code uses a generator object, that means that it can read any size of input file without wasting memory and time. after this, the "\n" sequence will stripped off without hassle
https://code.sololearn.com/cYQO6wGf8888/?ref=app
+ 5
Hirani Pranav , strip() is a string method that removes all preceding and trailing "whitespaces" in a string. whitespace is not only the space you are using to separate words, but also tabs "\t" and newline sequences "\n".
so in case of this book project, it makes us able to remove all the newline sequences from the end of a string when reading it from a file.
+ 4
file = open("/usercode/files/books.txt", "r")
books = file.readlines()
for book in books:
initial = book[0]
book = book.replace('\n', '')
char = len(book)
print(initial+str(char))
file.close()
+ 3
file = open("/usercode/files/books.txt", "r")
for line in file :
print(line[0]+str(len(line.strip())))
file.close()
+ 3
Yusril ,
what you are doing is not reading from the file, but just printing the expected result. this not what the exercise is made for.
+ 2
Lothar apparitiable 👏
+ 2
Thanks Lothar You said it before but I can't understand at that time.
But at this time it's all clear ☺️
+ 2
#your code goes here
list = file.readlines()
k = len(list)+1
newlist = []
i = 0
while i < k-1:
newlist.append(list[i])
i += 1
finallist = []
for element in newlist:
finallist.append(element.strip())
for line in finallist:
print(line[0]+str(len(line)))
+ 1
file = open("/usercode/files/books.txt", "a")
file.write("\n")
file.close()
file = open("/usercode/files/books.txt", "r")
#your code goes here
for line in file:
print(line[0]+str(len(line)-1))
file.close()
+ 1
file = open("/usercode/files/books.txt", "r")
list = file.readlines()
for i in list:
if i[-1] == "\n":
print(i[0] + str(len(i)-1))
else:
print(i[0] + str(len(i)))
file.close()
0
Glad I found this solution. I had the same issue with that newline.
0
Thanks Jeramy
0
file = open("/usercode/files/books.txt", "r")
for line in file:
print(line[0], end=' ')
print(len(line) - 1)
file.close()
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()
0
f = open("/usercode/files/books.txt", "r")
b = f.read()
x=b.split("\n")
for i in x:
l=len(i)
c=i[0]
print(c+str(l))
f.close()
0
I used replace to remove "\n" and replace it by ""
file = open("/usercode/files/books.txt", "r")
for line in file:
line1=line.replace("\n","")
print (line[0]+str(len(line1)))
file.close()
0
file = open("/usercode/files/books.txt", "r")
booklist = file.readlines ()
for book in booklist :
book = book.strip ()
print (book[0]+str(len(book)))
file.close()