How to read all lines from file and print only last word from each line?
Question:
I need a python code with split or re.findall to read lines from file start with #EXTINF: then remove complete line and keep only last word or sentence …
For example these line:
#EXTINF:-1 tvg-id="" tvg-name="|AR| beIN MOVIES 1 HD" tvg-logo="" group-title="OSN & BEIN-entre",|AR| beIN MOVIES 1 HD
#EXTINF:0,GLORY 77 BOX
#EXTINF:0 group-title="Tunisia"tvg-logo="http://logo.ddnb.tn/logochanel/1.png",National 1
#EXTINF:-1,Mera Farz | Superhit Hindi Action Movie | Amrishpuri ,Sri Devi , Nagarjuna
#EXTINF:1, FOOTBALL
#EXTINF:0,Aaryaa News
Answers:
Try this:
with open("sample.txt") as f:
print("n".join(l.rsplit(",")[-1].strip() for l in f.readlines()))
Output:
|AR| beIN MOVIES 1 HD
GLORY 77 BOX
National 1
Nagarjuna
FOOTBALL
Aaryaa News
I need a python code with split or re.findall to read lines from file start with #EXTINF: then remove complete line and keep only last word or sentence …
For example these line:
#EXTINF:-1 tvg-id="" tvg-name="|AR| beIN MOVIES 1 HD" tvg-logo="" group-title="OSN & BEIN-entre",|AR| beIN MOVIES 1 HD
#EXTINF:0,GLORY 77 BOX
#EXTINF:0 group-title="Tunisia"tvg-logo="http://logo.ddnb.tn/logochanel/1.png",National 1
#EXTINF:-1,Mera Farz | Superhit Hindi Action Movie | Amrishpuri ,Sri Devi , Nagarjuna
#EXTINF:1, FOOTBALL
#EXTINF:0,Aaryaa News
Try this:
with open("sample.txt") as f:
print("n".join(l.rsplit(",")[-1].strip() for l in f.readlines()))
Output:
|AR| beIN MOVIES 1 HD
GLORY 77 BOX
National 1
Nagarjuna
FOOTBALL
Aaryaa News