0

Working with Files module in Intermediate Python

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. To count the number of words in a given string, you can use the split() function, or, alternatively, count the number of spaces (for example, if a string contains 2 spaces, then it contains 3 words). Note: With each re-write of this project, the code got worse. At first, there's no input for the answer and the format appeared to be the same as the expected output but it was wrong. with open("/usercode/files/books.txt") as f: i = 1 for i in f: print(f"Line"+ i+": "+str(len(i.split()))+" words") All of the output is on one line and the variable i, since it is in f, is string instead of 1, 2, 3, etc. Getting book titles instead of line numbers is a first for me.

9th Feb 2024, 11:08 PM
Michael
50 Réponses
+ 1
Michael , Please add a tag for python.
9th Feb 2024, 11:16 PM
Rain
Rain - avatar
+ 1
Michael , The line i = 1 is unnecessary. To get the for loop to give you both the line number and the line itself, use enumerate. https://docs.python.org/3/library/functions.html#enumerate When you use an f-string, stay with the f-string and insert your variables inside it, instead of unnecessarily concatenating the f-string to your variables and converting stuff to str(). Watch your spaces. The output has to be exact. Try not to look below until you attempt a rewrite. with open("/usercode/files/books.txt") as f: for line, title in enumerate(f, 1): print(f"Line {line}: {len(title.split())} words")
9th Feb 2024, 11:27 PM
Rain
Rain - avatar
+ 1
The code was lost because I didn't copy it. I found out that the f.read or f.readline statement coding is very similar to the with open statement. I'll share with you on tomorrow.
17th Feb 2024, 4:26 AM
Michael
+ 1
I'm sorry, Rain. I'm not able to give a chance to see my code but I haven't forgotten. Thank you and good night or good morning.
18th Feb 2024, 2:12 AM
Michael
+ 1
I think there is a little unsolved problem, regarding readline() and readlines()? To my surprise, readlines() doesn't work like as my intuition. https://sololearn.com/compiler-playground/cUDV7QrY23dG/?ref=app
19th Feb 2024, 4:16 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Your code could look like this...😎: with open("/usercode/files/books.txt") as file: i = 1 for el in file: print(f"Line {i}:", len(el.split()),"words") i += 1
10th Feb 2024, 12:30 AM
Solo
Solo - avatar
0
Thank you, both for your advanced, beyond my comprehension input. My problem is no input in the input bar on the results page. My other problem is no input that would be read by the with open () statement which, maybe could have been a plain, stupid read() statement. My next problem is no output no matter what I do with a rewrite. I destroyed all new rewrites and trying to figure out how to take the original code I wrote here in this forum and make the results to not read on the same line. The results are to read next line, next line, etc. Thank you both for the advance lessons and the URL. On the enumerate () statement, I'm wondering why it was put in this forum as for line, title in enumerate (f,1) instead of for line, title in enumerate (f,start=1). I'm sure that both are okay but I wonder too much on what Sololearn has to say. I found out that print(f"Line ....") hasn't been necessary to use, too.
13th Feb 2024, 1:08 PM
Michael
0
Michael , Sorry you're still having trouble, but don't give up. All code makes sense eventually. You just have to figure out how each part works. There's not supposed to be any input shown in the test case, because you're not getting any data from the user with an input() call. You're only getting data from the books.txt file. Your original code pasted in the opening comment had this output because you used the same name i for two different variables, one outside the loop and one inside the loop. The i outside is your line counter and is unused. The i inside holds the text of one line of the file and is reassigned each iteration. LineHarry Potter : 2 words LineThe Hunger Games : 3 words LinePride and Prejudice : 3 words LineGone with the wind: 4 words The code I pasted had this output. Line 1: 2 words Line 2: 3 words Line 3: 3 words Line 4: 4 words I don't know what your new code looks like, so I can't analyze it. Will you save it in the regular playground and share the link here?
13th Feb 2024, 8:25 PM
Rain
Rain - avatar
0
Michael , The enumerate function has a default argument of 0 for its start parameter, which is why it looks like start=0 in the parameters list in the docs. If you don't send a second argument, it supplies its own argument and starts at 0. To send a second argument, you don't need to name it. You can just send the value, because it's what they call a "positional argument". In other words, since enumerate only has two parameters, if you send two arguments, the first argument gets assigned to the first parameter, and the second argument gets assigned to the second parameter. Named arguments are for functions that can take any number of positional arguments, such as print(). For example, the only way print can know that an argument is supposed to be assigned to the sep parameter is if you name it in the call. Otherwise, it would think it was a positional argument and print it normally. print(3, sep=".") # 3 print(3, 9, sep=".") # 3.9 print(3, 9, 16, sep=".") # 3.9.16 print(3, 9, 16, ".") # 3 9 16 .
13th Feb 2024, 8:40 PM
Rain
Rain - avatar
0
Okay, I understand the enumerate statement but like my SQL Intermediate question that was asked months ago on what isn't working and lessons being built with extra, out of school, out of the ordinary requests. Where are the real data scientists, data architects, software engineers, etc. because a select few know what I'm talking about and if you want to argue, bring it.
17th Feb 2024, 1:25 AM
Michael
0
Again, I need a software named version, its compiler, and I don't care if you say GNU. You know what I'm asking and my question is still the same question and forget you on enterprise services and secrets. You told Google's and a few other enterprises including Oracle. May I guess because you will not respond?
17th Feb 2024, 1:29 AM
Michael
0
Do I have to worry about something more than a space or NULL in a ASCII char format? I'm strictly the basics and thank everyone for your help, answers, and support.
17th Feb 2024, 1:32 AM
Michael
0
Question. Forget the with open statement. Am I able to do this readlines project without using with open and use f.read statement?
17th Feb 2024, 1:36 AM
Michael
0
Michael , Are you asking me? You can address a comment by typing @ and choosing the person's name from a pop-up list. I don't know anything about your SQL question. I"m just responding to this discussion here. Yes, you can open a file in Python without using with, if you use the open() function alone. I'm pretty sure they taught that before they taught using with. But since with automatically closes the file, you'll need to explicitly close the file yourself using my_file.close() if you don't use with. And yes you can use file methods to grab the text, such as: my_text_line = my_file.readline() Here are some docs about the file methods. https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects
17th Feb 2024, 2:22 AM
Rain
Rain - avatar
0
Yes, I do need to address questions two and three to management and not you, sir or madame. I know that this is supposed to be an open forum and I'm too certain that everything and everyone is followed or is being followed on this app for learning purposes and more. I didn't know about the ampersand (@) pop-up list but I just seen the preview. Thank you for your help, knowledge, and expertise. My output is still on one line, as usual. As usual, I took everything down and did a new re-write. I think that I work better with a fresh, new re-write after reviewing the lesson material, again, before the project.
17th Feb 2024, 2:38 AM
Michael
0
Sir or Madame Rain, may I ask you a coding question?
17th Feb 2024, 2:43 AM
Michael
0
Thank you for the URL. It had a lot of meaning to me.
17th Feb 2024, 2:44 AM
Michael
0
Michael , Rain is an aspect of the water elemental, so is technically sexless, but the host body it is currently possessing for the purposes of communication happens to be human male. I can't really check your code unless you save it and share the link.
17th Feb 2024, 2:56 AM
Rain
Rain - avatar
0
Did you know that water is a female? That's why I said sir or madame. I'm internet dumb.
17th Feb 2024, 2:58 AM
Michael
0
Michael , No, I didn't. Thinking about it, I guess it depends on the language, because, Spanish agua female, fumo male, but English water and smoke no difference.
17th Feb 2024, 2:59 AM
Rain
Rain - avatar