+ 2
How to solve the Book Titles in python??
Tried a lot bud didn't succeeded. Here's the code I tried - file = open("/usercode/files/books.txt", "r") data = file.readlines() amount1 = file.write(data[0][0]) amount2 = file.write(data[1][0]) amount3 = file.write(data[2][0]) amount4 = file.write(data[3][0]) print(data[0][0] + str(amount1)) print(data[1][0] + str(amount2)) print(data[2][0] + str(amount3)) print(data[3][0] + str(amount4)) file.close()
15 odpowiedzi
+ 22
Here is the answer i guess it's pretty clear
file = open("/usercode/files/books.txt", "r")
titles = file.readlines()
for i in titles:
if '\n' in i:
print(i[0] + str(len(i) - 1))
else:
print(i[0] + str(len(i)))
file.close()
+ 5
file = open("/usercode/files/books.txt", "r")
list = file.readlines()
for i in list:
i = i.replace("\n","")
print (i[:1]+str(len(i)))
file.close()
+ 2
Thank you Lemesa
+ 2
file = open("/usercode/files/books.txt", "r")
for line in file:
if '\n' in line:
print(line[0]+str(len(line)-1))
else:
print(line[0]+str(len(line)))
file.close()
+ 1
Hi! For better help to you, please, show us your code attempt! Thx!
+ 1
Also
for line in file:
print (line[0]+str(len(line.strip())))
+ 1
A more compact solution.
file = open("/usercode/files/books.txt", "r")
for i in file.readlines():
print(i[0]+str(len(i)-1)) if "\n" in i else print(i[0]+str(len(i)))
file.close()
+ 1
if anyone is looking at this post today in my opinion one of the simplest solutions would be:
file = open("/usercode/files/books.txt", "r")
#your code goes here
for x in file:
book = x[0] + str(len(x.strip("\n")))
print(book)
file.close()
0
Thanks Lemesa
0
Thank you so much! I have been taking notes all this time. I wouldn't have been able to figure it out on my own.
0
Turns out, all I needed was that "\n". :)
That little detail kept me up all night.
Thanks a bunch
0
Thanks Lemesa. I appreciate your advice. Your tips will be very useful for me as a beginner and for someone who wants to learn python. I am also currently studying at the university and writing research on EU law. At https://writix.co.uk/essay-examples/supremacy-of-eu-law I found very useful information about EU law in general and its supremacy.
0
This is my answer for this :
for i in range(len(a)):
x=len(a[i])
if "\n" in a[i]:
x -=1
x=str(x)
print(a[i][0] + x)
i+=1
0
Thank you
0
This is how I solved the problem:
file = open("/usercode/files/books.txt", "r")
f = file.readlines()
#your code goes here
for x in f:
if x==f[-1]:
print(x[0] + str(len(x)))
else:
print(x[0] + str(len(x)-1))
file.close()
To solve this I first tried:
file = open("/usercode/files/books.txt", "r")
for x in file:
print(x[0] + str(len(x))
file.close()
This correct, only for the last book title in the file, which did not include "\n". All the titles previous were off by one number because they included the "\n" after the name.
Then I tried:
file = open("/usercode/files/books.txt", "r")
for x in file:
if x==file[-1]:
# isolating last item in file
print(x[0] + str(len(x))
file.close()
This did not work and gave me an error because in order to index a file, you gave to make it a list and this can be done using "readlines()".
Hence, I came up with:
file = open("/usercode/files/books.txt", "r")
f = file.readlines()
#your code goes here
for x in f:
if x==f[-1]:
print(x[0] + str(len(x)))
else:
print(x[0] + str(len(x)-1))
file.close()
...and it worked!