HomeОбразованиеRelated VideosMore From: Dominique Thiebaut

Parsing Text Files in Python

404 ratings | 113481 views
A short program to read lines from a text file and extract information, patterns, from each line.
Html code for embedding videos on your blog
Text Comments (28)
oluwapeminsin Awolesi (15 days ago)
please can you make a dictionary program that gets information from a file please!!!! like you save a .txt file then you add some words to the .txt file so the like apple : an apple is a fruit then the program ask for an input then you type in apple and it prints out an apple is a fruit please i am new to programming and i have been struggling with this for some months now
Billy (15 days ago)
Can you show me a way to have text file with a = 10.35 b = 20.57 c = 30.79 on separate lines and get program to only read the variable decimal values and assign them to variables a, b, c in the program.
print compu (5 months ago)
how make it wark in multiple files in one der
Jovan Djordjevic (9 months ago)
line.find(' NO '), i think you are complicating it a lil bit
Gatya (1 year ago)
if 'yes' in line : countYes += 1 if 'no' in line: countNo += 1
M.Ammar Nasim (1 year ago)
how did you make demoFileRead.py???
José R (1 year ago)
and for read paragraph?
Juan Fajardo (1 year ago)
You can see a paragraph like a big line
Muhamed Kasim (1 year ago)
how can calculate line with out (for)
this last variant): nice task text = 'yesqqqyespppnoppyesyes' text_yes = [text[ind:ind+3] for ind,letter in enumerate(text) if letter == 'y' and text[ind:ind+3] == 'yes'] text_no = [text[ind:ind+2] for ind,letter in enumerate(text) if letter == 'n' and text[ind:ind+2] == 'no'] print('YES:',len(text_yes)) print('NO:',len(text_no))
I find this, what you think is it good? text = 'yesqqqyespppppyesyes' for ind, letter in enumerate(text): if letter == 'y' and text[ind:ind+3] == 'yes': print('wow')
@Dominique Thiebaut Thank you I did a litle another way with open('txt','r') as f: text = f.read().split() f.close text = [word.lower() for word in text] print('YES: ',text.count('yes'),' NO: ',text.count('no')) but I still have question how can I count "yesyes"?
Dominique Thiebaut (1 year ago)
If you do not want a for loop, you could put the contents of the file into a single string using the read() function, and then you can search the whole contents with the index() function.
Malav Soni (1 year ago)
every time i try it i get this "syntax error" and that it is not apprehended in this piece of code that is given please help!! unfortunately i cannot upload a image ("print screen") of that otherwise i would. thank you any way. peace
Rud Dog (2 years ago)
Where would I learn what features are available in the form of imported modules. It was not till recently that I discovered "OS". What modules are available that can recover information about files size, type, etc? Also how does one find out if the modules are cross platform, Mac, PC and Unix? Thank you for your clear and to the point videos on Python 3. My search continues for same type videos but on tkinter.
Mustafa Uçar (2 years ago)
ağzını şıplatma hoca
Sina Mir Mahmoud (4 months ago)
shiplatirsa ne olur lan?
nuradin abdi (2 years ago)
keyboard noises make ME CRINGE
Jesus Covarrubias (2 years ago)
Thanks for pointing that out.
Jordan Taylor (2 years ago)
i hammer keys too, probably still habituated from old consoles in the 80's. it'll get better when we all die off in 30 years.
Ali Alghamdi (2 years ago)
The "te" sounds
nuradin abdi (2 years ago)
shane butler (3 years ago)
thank you sir
Malik Rumi (3 years ago)
WAIT A MINUTE! You're not going to deal with or explain the two YES on one line? What if someone wants all the YES, not just a count of lines with YES in them?!
Gatya (1 year ago)
well you see its counting per line, it checks per line if there is a yes or a no, if it does find one in a line, the code below will execute and increment the count for yes or no, because it checks per lines not per words
Mark Walle (2 years ago)
You could use count instead of find, so like: countYes += line.count("YES") According to help(str.find) and help(str.count): Find will Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Count will return the number of non-overlapping occurrences of substring sub in string S[start:end].
confidential303 (2 years ago)
Need help on python I want to access a media box harddisk (which has an internal address like ) and list the files within the directory ..All my recorded video is listed in the map /media/movies I want to create a script that can list all the files. is this possible ? or should you turn to other languages ??
the flavabomb (3 years ago)
exactly what I was thinking...the whole time I was like...HE. MUST SEE THERE IS 4! 😂

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.