Intermediate python working with files | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Intermediate python working with files

soRRY ABOUT THE CAPS BUT MY DEVICE WONT TURN THENCAPS OFF IM STUCK FOR THE THREE LESSONS READING THROUGH BOOK CLUB FILLINGBUP WITH NUMBERS THEY ARE FAILING ON ALL CASES BOOK CLUB file = open("/usercode/files/books.txt", "r") for line in file: nums= list(line) x=len(nums)-1 print(line[0]+str(x)) file.close()

17th Feb 2021, 7:05 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
10 Answers
+ 8
I have made it! line_num = 0 with open("/usercode/files/books.txt") as f: for line in f.readlines(): line_num += 1 print("Line"+" "+str(line_num)+":"+" "+str(len(line.split()))+" "+"words") f.close()
1st May 2021, 9:01 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
+ 5
with open("/usercode/files/books.txt") as f: #your code goes here for count, line in enumerate(f.readlines()): print(f'Line {str(count+1)}: {str(len(line.split()))} words')
10th Sep 2021, 3:05 PM
Challen Wang
Challen Wang - avatar
+ 3
with open("/usercode/files/books.txt") as f: #your code goes here words = f.readlines() for x in (range(len(words))): print("Line", str(x+1)+":", len(words[x].split()),"words")
9th Nov 2022, 1:04 PM
Laurent
+ 2
I have an answer on your other similar thread. https://www.sololearn.com/Discuss/2699121/?ref=app
17th Feb 2021, 8:44 AM
noteve
noteve - avatar
+ 2
with open("/usercode/files/books.txt") as f: #your code goes here amountWords = 0 lines = 0 for line in f: lines += 1 amountWords += 1 print("Line" + " " + str(lines) + ":" + " " + str(len(line.split())) + " " + "words") f.close()
3rd Jan 2022, 7:19 PM
Nazarii Zavada
Nazarii Zavada - avatar
+ 1
here is the description You are given a books.txt file, which includes book titles, each on a separate line. Create a program to output how many words each title contains, in the following format: Line 1: 3 words Line 2: 5 words ... Make sure to match the above mentioned format in the output.
17th Feb 2021, 7:09 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
+ 1
A little tweak on Swapnil Kamdi's code, it is correct, but you don't have to + on spaces, spaces inside Quotation mark does count. Example: print("Line"+" "+str(line_num)+":"+" "+str(len(line.split()))+" "+"words") Tweaked example: print("Line " + str(line_num) + ": " + str(len(line.split())) + " words") Note: Not that big of a deal, but could make the code easier to read. Tweaked code: line_num = 0 with open("/usercode/files/books.txt") as f: for line in f.readlines(): line_num += 1 print("Line " + str(line_num) + ": " + str(len(line.split())) + " words") f.close()
6th Aug 2021, 11:41 AM
Tommy Halvorsrud
Tommy Halvorsrud - avatar
0
not sure how to do line 1: h12 and the other three lines
17th Feb 2021, 7:07 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
0
line_num = 0 with open("/usercode/files/books.txt") as f: for line in f.readlines(): line_num += 1 print("Line"+" "+str(line_num)+":"+" "+str(len(line.split()))+" "+"words") f.close()
23rd Aug 2021, 4:00 PM
Vraj Soni
Vraj Soni - avatar
0
My code is here with open("/usercode/files/books.txt") as f: #my code text=f.readlines() for i in range(len(text)): print(f"Line {i+1}:",len(text[i].split()),"words") i+=1
17th Dec 2023, 3:28 PM
vahid kamrani
vahid kamrani - avatar