[Tutor] String Attribute
Alan Gauld
alan.gauld at btinternet.com
Sat Aug 1 10:14:27 CEST 2015
On 01/08/15 00:59, ltc.hotspot at gmail.com wrote:
>> for line in fh:
>> line2 = line.strip()
>> line3 = line2.split()
>> line4 = line3[0]
>
> →→Apparently, the data content in the file is lost from the address sort function to line2? :
It is not lost, it is an empty line.
> In [47]: print line2.split()
> []
split has returned no content.
The line must have been empty (or full of whitespace
which strip() removed).
> In [48]: print line2
> In [49]: print line.strip()
Again it shows an empty line.
> 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'])
But this is odd since it shows the set containing the full line which
suggests you maybe did an add(line) instead of add(line4) at some point?
> You have removed the code that tested for the first
> word being "From". You should put that check back in your code.
If you do this it should fix the IndexError problem too,
since empty lines will not start with From
ie your loop should look like
for line in fh:
if line.startswith('From'):
# the loop body as it currently is
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list