[Tutor] String Attribute

ltc.hotspot at gmail.com ltc.hotspot at gmail.com
Thu Jul 30 21:07:33 CEST 2015








Sent from Surface





From: ltc.hotspot at 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.hotspot at gmail.com
Cc: Tutor at python.org





On Tue, Jul 28, 2015 at 11:33:53PM +0000, ltc.hotspot at 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


More information about the Tutor mailing list