[Tutor] String Attribute

ltc.hotspot at gmail.com ltc.hotspot at gmail.com
Sat Aug 1 01:25:11 CEST 2015


Emile,


--> Captured is a printout from line3 to addresses,  below:



In [46]: print line3
[]


In [47]: print line2.split()
[]


In [48]: print line2


In [49]: print line.strip()


In [50]: print fh
<open file 'mbox-short.txt', mode 'r' at 0x00000000035CB0C0>


In [51]: print addresses
set(['1.0', 'source at collab.sakaiproject.org;', 'Jan', 'mail.umich.edu', 'Innocen
t', '0.0000', 'CMU', 'frankenstein.mail.umich.edu', '0.8475', 'from', 'source at co
llab.sakaiproject.org', '05', '<200801051412.m05ECIaH010327 at nakamura.uits.iupui.
edu>', 'flawless.mail.umich.edu', '5', 'nakamura.uits.iupui.edu:', 'shmi.uhi.ac.
uk', '7bit', 'text/plain;', '<source at collab.sakaiproject.org>;', 'Sat,', 'nakamu
ra.uits.iupui.edu', 'paploo.uhi.ac.uk', 'FROM', 'holes.mr.itd.umich.edu', '(from
', '<postmaster at collab.sakaiproject.org>', '[sakai]', 'stephen.marquard at uct.ac.z
a', 'Sat'])


In [52]:



→Original Traceback error message:



IndexError                                

Traceback (most recent call last)


C:\Users\vm\Desktop\apps\docs\Python\_8_5_v_21.py in <module>()
      7         line2 = line.strip()
      8         line3 = line2.split()
----> 9         line4 = line3[1]
     10         addresses.add(line4)
     11         count = count + 1




IndexError: list index out of range



→ Latest code printout:


fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
addresses = set()
for line in fh:
 line2 = line.strip()
 line3 = line2.split()
 line4 = line3[1]
 addresses.add(line4)
 count = count + 1
print "There were", count, "lines in the file with From as the first word"
print addresses



→ I swapped line statements between lines 4 & 5.  No change on the Traceback error message:


IndexError                                

Traceback (most recent call last)
C:\Users\vm\Desktop\apps\docs\Python\_8_5_v_21.py in <module>()
      8         line2 = line.strip()
      9         line3 = line2.split()
---> 10         line4 = line3[1]
     11         addresses.add(line4)
     12         count = count + 1


IndexError: list index out of range


In [54]: print line3


→Question: The file data content is lost on execution of the sort function?


Regards,

Hal






Sent from Surface





From: Emile van Sebille
Sent: ‎Friday‎, ‎July‎ ‎31‎, ‎2015 ‎4‎:‎16‎ ‎PM
To: Tutor at python.org





On 7/31/2015 11:57 AM, ltc.hotspot at gmail.com wrote:

> →Question:  Why is the list index out of range on line # 9:
>
> IndexError
>
>   Traceback (most recent call last)
> C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_20.py in <module>()
>        7         line2 = line.strip()
>        8         line3 = line2.split()
> ----> 9         line4 = line3[1]
>       10         addresses.add(line4)
>       11         count = count + 1
> IndexError: list index out of range
>

Because line3 is not sub-scriptable.

Have you examined what line3 holds when the error occurs?

Emile



_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list