Hi everyone,
Revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[1] fn.seek(0) print sender Questions: Why is the loop not repeating, and where should I insert a split to remove 'Sat Jan 5:09:14:16 2008' From stephen.marqu...@uct.ac.za Sat Jan 5 09:14:16 2008 ← Mismatch Sent from Surface From: ltc.hots...@gmail.com Sent: Thursday, July 30, 2015 12:07 PM To: Tutor@python.org Sent from Surface From: ltc.hots...@gmail.com Sent: Thursday, July 30, 2015 11:47 AM To: Steven D'Aprano Hi Steve: New revision code: count = 0 fn = raw_input("Enter file name: ") if len(fn) < 1 : fname = "mbox-short.txt" for line in fn: if 'From' in line.split()[0]: count += 1 print "There are %d lines starting with From" % count print len(line) fn = open(fname) print "There were", count, "lines in the file with From as the first word" Syntax message produced by iPython interperter: NameError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_2.py in <module>() 6 print "There are %d lines starting with From" % count 7 print len(line) ----> 8 fn = open(fname) 9 print "There were", count, "lines in the file with From as the first wor d" NameError: name 'fname' is not defined In [16]: Question: Why is fname = "mbox-short.txt" not loading the sample data? Sample data file is located at http://www.pythonlearn.com/code/mbox-short.txt Regards, Hal Sent from Surface From: Steven D'Aprano Sent: Wednesday, July 29, 2015 7:42 AM To: ltc.hots...@gmail.com Cc: Tutor@python.org On Tue, Jul 28, 2015 at 11:33:53PM +0000, ltc.hots...@gmail.com wrote: > > Hi Everyone: > > What is the source of the syntax error to the String Attribute? > > Go to the following URL links and view a copy of the raw data file code and > sample data: Please don't send people to URLs to view your code. Copy and paste it into the body of your email. > 1.) http://tinyurl.com/p2xxxhl Running the code in the simulator, I get the following error on line 6: AttributeError: 'str' object has no attribute 'startwith' You misspelled "startswith" as "startwith" (missing the second "s"). -- Steve _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor