How to Extract Lines from Text File Using Python | Python3 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to my youtube channel and in this video we are going to discuss that how we can extract lines from text file using python now if you don't know that what is python and if you don't have any idea that how to do programming using python then i have designed a complete python course just for beginners and you can get that course on udemy.com now what you have to do is go to udb.com and grab this course which is python for beginners learn python from scratch and python 3. now in this course we'll start learning python from scratch and after that we'll go up to the top and learn advanced level programming using python language now you can get this course for 69.99 on udemy.com but if you go down here and click on apply coupon and enter discount and click on apply then you can get this course for just 9.99 which is 86 off of the original price 69.99 now i will leave the link of this course in the description below and i will also attach the coupon link and coupon code in the description of this video so you can click on that link and grab your 86 percent discount on this course now without further ado let's just dive into the tutorial self [Music] so in this lecture we're gonna see that how we can manipulate and handle a file which contains the text consisting on the multiple lines for example i have taken this file which is named as document.txt i will open up this file and you can see that this file has all these strings in it you can see that there is a specific line at the start of each paragraph and that line starts from subject right you can see that the subject is written here too and here too and all the way till the end of this file so i will use this file as an example and i will try to manipulate this file and i will try to print the lines which starts from subject so what i'll do first of all i will just go ahead and move to the folder where i have saved my file and then i will list the items to see that if i can see my file here so here you can see the name of your file which is document document.txt what i'll do i will just clear out this terminal and i will create a file here named as file processing dot py and you can see that the file named as file underscore processing.py has been created so what i'll do first of all i will suppose a variable and in that variable i will open up my file and the name of the file was document dot txt so this statement is actually asking to open up the file which is named as document.txt and then saving that file inside this variable so in order to verify that my file has been assigned to that variable or not i will print out that variable i will save this file and i will execute this file using python now you can see that instead of printing out the entire file it just showed me that the file named as document txt has been opened up in reading mode right so what i want to do is i want to print all the lines which start from subject word which means that i have to use the concept of prefixes now for that i have to use a function called starts with first of all i will define a loop in which i will look for line inside the variable x and this variable x contains the entire file in it i will write colon operator and then i will hit enter now inside this loop i will go ahead and define a conditional statement and i will say that if the line starts with and in the parenthesis i will define the prefix which is subject [Music] then print that line after that i will get outside of this conditional statement and i will again hit backspace and get outside of the loop body here i will print out [Music] the process is done i will save this file and i will try to execute this program using python here you can see that all the lines which was started from the word subject has been printed out here skipping all other lines that were present in the text file and after that the process is done is printed just like we asked this program to do at the end so that is how we can use open function to open up a file and then we can go through that entire file using a for loop and we can also extract some specific lines on the basis of their prefixes
Info
Channel: Muhammad Talha
Views: 1,358
Rating: 4.826087 out of 5
Keywords: text processing, text extraction, Line extraction using python, Python code, extract text from image, linux, Python course, Python programming, Python Coding, Learn Python, Udemy course, Prefixes in python, Python prefixes, How to extract lines from text file, Python automation, Python tutorial, Python Udemy course, Python 2021, Python3 tutorial, machine learning, nlp and text processing, complete text processing, Python text processing, Python file handling
Id: ufaC7ZuXDnI
Channel Id: undefined
Length: 5min 51sec (351 seconds)
Published: Wed Mar 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.