Skip to content

Instantly share code, notes, and snippets.

@MichelleDalalJian
Created October 7, 2017 12:54
Show Gist options
  • Select an option

  • Save MichelleDalalJian/341754de0175074cffb59ab3490da032 to your computer and use it in GitHub Desktop.

Select an option

Save MichelleDalalJian/341754de0175074cffb59ab3490da032 to your computer and use it in GitHub Desktop.
8.5 Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line: From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out…
fhand = open("mbox-short.txt")
count = 0
for line in fhand:
line = line.rstrip()
if line == "": continue
words = line.split()
if words[0] !="From": continue
print(words[1])
count = count+1
print ("There were", count, "lines in the file with From as the first word")
@saratorabi-github

Copy link
Copy Markdown

I have the same problem with 54 lines ,anybody can help?

ghost commented Nov 8, 2021

Copy link
Copy Markdown

count=0
fname = input("Enter file name: ")
try:
fh=open(fname)
except:
print("file does not exist")
for line in fh:
line=line.rstrip()
words=line.split()
if len(words) <=3 or words[0] != 'From':
continue
else:
print(words[1])
count=count+1
print("There were",count, "lines in the file with From as the first word")

@yuriszc

yuriszc commented Nov 22, 2021

Copy link
Copy Markdown

fname = input("Enter file name: ") # input from user
try: # try to open the file name inputed by user
ofile = open(fname) # open the file
except: # if something wrong do this
print("Impossible to find", fname, ", please try to search other file!") # feedback message
quit() # finish the program

count = 0 # valid line counter

for line in ofile: # loop to check all file lines
if not line.startswith("From "): # loop conditional
continue # if != of conditional restart the loop
else: # if matchs the conditional do it:
count = count + 1 # count the lines
line = line.strip().split() # remove blank spaces/lines and split it to creat a list
print(line[1]) # print the second item of the list

print("There were", count, "lines in the file with From as the first word") # print the final informations as resquested

@nguyen-hang

Copy link
Copy Markdown

fn = input("Enter file name: ")
fname = open(fn)
count = 0
for line in fname :
.....if not line.startswith('From ') : continue
.....lst = line.rstrip().split()
.....for text in last :
..............if '@' in text :
......................count = count + 1
......................print(text)

print('There were', count , 'lines in the file with From as the first word')

@BellyRash

Copy link
Copy Markdown

fname = input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
for line in fh:
line = line.rstrip()
if not line.startswith('From '):
continue
words = line.split()
email = words[1]

print(words[1])

count = count + 1

print("There were", count, "lines in the file with From as the first word")
`

@VinayReddy-Vangala

VinayReddy-Vangala commented Feb 11, 2022

Copy link
Copy Markdown

@mohammedtahir07 @saratorabi-github @rahmioruc hey u guys gave a wrong comparing string : try giving space after 'From ', you guys have skipped giving space after 'From '
refer my below code
python 8 5
)

code
fname = input("Enter file name: ")
if len(fname) < 1:
fname = "mbox-short.txt"

fh = open(fname)
count = 0
for line in fh:
if line.startswith('From '):
count+=1
split=line.split()
print(split[1])

print(f"There were {count} lines in the file with From as the first word") #using f-string

@dashp

dashp commented Feb 13, 2022

Copy link
Copy Markdown

You can try this approach as well.

f_name = input("Enter FileName: ")
count = 0
if len(f_name) < 3:
f_name = "mbox-short.txt"
try:
fh = open(f_name)
except:
print("Check FileName")
for line in fh:
word = line.split()
if len(word) < 2:
continue
if word[0] == "From":
count = count + 1
print(word[1])
print("Count", count)

@VinayReddy-Vangala

VinayReddy-Vangala commented Feb 13, 2022 via email

Copy link
Copy Markdown

@dashp

dashp commented Feb 14, 2022

Copy link
Copy Markdown

line.split() - by default split does not preserve spaces. You dont really need strip() with split().

@xassan-cpu

Copy link
Copy Markdown

image

@Sirilukkan

Copy link
Copy Markdown

I would like to ask what is the role of the command below?

if len(fname) < 1:
fname = 'mbox-short.txt'

I think we do not need to specify, but I saw the code example and see that they have these two lines, which I am not sure how useful this is.

fname = input("Enter file name: ")
if len(fname) < 1:
fname = 'mbox-short.txt'
fh = open(fname)
count = 0
for line in fh:
line = line.rstrip()
if not line.startswith('From: ') : continue
words = line.split()
print(words[1])
count = count + 1
print("There were", count, "lines in the file with From as the first word")

@emadhamdouna

Copy link
Copy Markdown

HELP: what's wrong with my code? :

fname = input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" text = open(fname) count = 0 for line in text: if not line.startswith("From"): continue else: count = count + 1 line = line.split() line = line[1] print(line)

print("There were", count, "lines in the file with From as the first word")

Did you know what was wrong because I have the same issue?

@ursrahuladhikari

Copy link
Copy Markdown

image

Editable code:
fname = input("Enter file name: ")
if len(fname) < 1:
fname = "mbox-short.txt"

fh = open(fname)
count = 0

for line in fh:
line=line.rstrip()
if not line.startswith('From:'):continue
words=line.split()

print (words[1])
count=count+1

print("There were", count, "lines in the file with From as the first word")

@MaryamFarshbafi

Copy link
Copy Markdown

fname = input("Enter file name: ")
if len(fname) < 1:
fname = "mbox-short.txt"

fh = open(fname)
count = 0
list=[]
for line in fh:

if not line.startswith('From:'):
        continue
line = line.rstrip()
list=line.split()
count+=1
print(list[1])

print("There were", count, "lines in the file with From as the first word")

@lazurs1

lazurs1 commented Aug 21, 2022

Copy link
Copy Markdown

Can some of you try rerunning your code? I think somethings is wrong with the site- I tired your code (after checking mine and exporting data to exell and doing a match to see if it was good) and the site keeps erroring my code and many of yours.

@MaryamFarshbafi

MaryamFarshbafi commented Aug 22, 2022 via email

Copy link
Copy Markdown

@faranakR

Copy link
Copy Markdown

fname = input("Enter file name: ")
if len(fname) < 1:
fname = "mbox-short.txt"

fh = open(fname)
count = 0

for line in fh:
if "From" in line and "From:" not in line:
lst = line.rstrip().split()
print(lst[1])
count += 1

print("There were", count, "lines in the file with From as the first word")

@lazurs1

lazurs1 commented Aug 24, 2022 via email

Copy link
Copy Markdown

@rishabhrathore055

Copy link
Copy Markdown

8.5 Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line:

From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008

You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message).

fname = input("Enter file name: ")
if len(fname) < 1:
    fname = "mbox-short.txt"

fh = open(fname)
count = 0
for line in fh:
    line = line.rstrip()
    if line == "":continue
    words = line.split()
    if words[0] =='From':
        print(words[1])
        count+=1

print("There were", count, "lines in the file with From as the first word")

@Jayanthpython

Jayanthpython commented Oct 11, 2022 via email

Copy link
Copy Markdown

@lazurs1

lazurs1 commented Oct 11, 2022 via email

Copy link
Copy Markdown

@longcheng2022

Copy link
Copy Markdown

Screen Shot 2021-07-07 at 3 46 45 PM

Could you tell me why we need "len(fname)<1"?

@CarlosWy

CarlosWy commented Nov 2, 2022

Copy link
Copy Markdown

I have the same problem with 54 lines ,anybody can help?

In the txt, there are two types of lines starts with 'From'

  1. From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
  2. From: stephen.marquard@uct.ac.za
    The question require us to pick out line type 2
    The code should be like:

截屏2022-11-02 12 42 20

@DaryllC

DaryllC commented Dec 28, 2022

Copy link
Copy Markdown

Hello I got the exact output but I don't have any clue to find the error itself, can anyone help me?

image

@Pirulito1tabello

Copy link
Copy Markdown

This one worked for me:

#Open the file mbox-short.txt
fname = input('Enter files name: ')
fop = open(fname)
count = 0
#read it line by line
for line in fop:
#When you find a line that starts with 'From
if line.startswith('From'):
if not line.startswith('From:'):
count += 1
#You will parse the From line using split()
for word in line.split():
#Then print out a count at the end
if '@' in word:
wrd = word
print(wrd)
print("There were", count, "lines in the file with From as the first word")
Screen Shot 2023-02-24 at 2 38 51 PM

@ali6406

ali6406 commented Mar 16, 2023

Copy link
Copy Markdown

fname = input("Enter file name: ")
fh = open(fname)
count = 0
for line in fh :
if not line.startswith("From ") : continue
words = line.split()
print(words[1])
count = count + 1
print("There were", count, "lines in the file with From as the first word")

@ShuckZ77

Copy link
Copy Markdown

fname = input("Enter file name: ")
of = open(fname) #mbox-short.txt

count=0

for lines in of:
if lines.startswith('From '):

    count= count+1
    
    lne1=lines.strip()
    lne2=lne1.split()
    
    print(lne2[1])

print("There were",count, "lines in the file with From as the first word")

@Amita1408

Copy link
Copy Markdown

fname=input('enter a file name:')
file=open(fname)
c=0
for i in file:

if i.startswith('From '):
    c=c+1
    print(i.split()[1])

print('There were',c, 'lines in the file with From as the first word')

@AramisCell

Copy link
Copy Markdown

The Best Option

filee = open(input("Enter file name: "))
count = 0
for line in filee:
if not line.startswith("From"): continue
words = line.split()
if words[0] == "From:":conti
print(words[1])
count = count + 1
print("There were", count, "lines in the file with From as the first word"

@nessKenya

Copy link
Copy Markdown

I would like to ask what is the role of the command below?

if len(fname) < 1: fname = 'mbox-short.txt'

I think we do not need to specify, but I saw the code example and see that they have these two lines, which I am not sure how useful this is.

fname = input("Enter file name: ") if len(fname) < 1: fname = 'mbox-short.txt' fh = open(fname) count = 0 for line in fh: line = line.rstrip() if not line.startswith('From: ') : continue words = line.split() print(words[1]) count = count + 1 print("There were", count, "lines in the file with From as the first word")

if you are prompting the user to enter the file name, you don't need again to assign the file name. Compare my image below....

need

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment