- 1

How do I read through a text file and store certain information?

Hi! I'm relatively new to python and am stuck in a problem. How do I store certain information from a text file into a global dictionary, list, and variables? The text file would be a book. Any help would be great! Thanks!

17th Oct 2020, 10:57 PM
Noob-San
Noob-San - avatar
8 Answers
+ 4
have you tried taking the python course? there are a few lessons about working with files. https://www.sololearn.com/learn/Python/2445/
17th Oct 2020, 11:18 PM
Евгений
Евгений - avatar
+ 3
I mean, SoloLearn's python course already have answer to your question, you just need to move on.
17th Oct 2020, 11:34 PM
Евгений
Евгений - avatar
0
I reviewed your link and unfortunately it does not have the answer to my problem. Also I already know how to open and read through a file categorizing the information in said file is the problem I'm having. The text analyzer helped me a bit for another section, but not the question I ask. Thanks though.
18th Oct 2020, 3:22 AM
Noob-San
Noob-San - avatar
0
Please explain with an example what kind of information the file has how you want it to be stored in a dictionary.
18th Oct 2020, 5:41 AM
abhinav
abhinav - avatar
0
That's the thing, my teacher didn't specify. She only said to read through a file and store some of the required information into global dictionaries, lists, and variables. And the text file to read through is a book. A literal whole book. She attached some history books for us as text files. For instance Founding Mothers and Fathers, Norton. There's no information on required text, so I'm assuming each chapter number and the chapter's information goes in dictionary and lists is the list of chapters and I don't know about variables though. If you say why not ask her. Apparently she's said that some people bullshit our last project and some people even try(incomplete work) so she's refusing to help at all. Pretty petty in my opinion.
18th Oct 2020, 5:49 AM
Noob-San
Noob-San - avatar
0
May be only the headings need to be stored with their numbers. To do so: book = open ( "book.txt" ) dict = { } for line in book.readlines ( ) : if line [ :-1 ].isupper ( ) : # assuming capital headings h = line [ :-1 ].split ( maxsplit = 1 ) dict [ int ( [ h [ 0 ] ] ) ] = h [ 1 ]
18th Oct 2020, 6:15 AM
abhinav
abhinav - avatar
0
Unfortunately that didn't work. Thanks for replying though. Also I just figured out what she wants in dictionary, lists and variables. I looked through all the books and found they have some stuff in common. Hopefully I'm assuming correctly.
18th Oct 2020, 7:10 AM
Noob-San
Noob-San - avatar
- 1
I'm currently learning python through school. My teacher did not go in depth with dictionary so I don't know how to do she expects me write this part of the code. I tried googling, reading different type of codes, and diving straight in but I still can't figure out how to store only certain information into those categories.
17th Oct 2020, 11:21 PM
Noob-San
Noob-San - avatar