[Tutor] String Attribute

Ltc Hotspot ltc.hotspot at gmail.com
Sun Aug 2 11:15:26 CEST 2015


Hi Alan,

Question1: Why did the following strip function fail:         line2 =
line.strip (',')
View instructions for 'str.strip([*chars*])¶
<https://docs.python.org/2.7/library/stdtypes.html?highlight=strip#str.strip>'
which is available at
https://docs.pythonorg/2.7/library/stdtypes.html?highlight=strip#str.strip

Question2: How do I code a vertical column output

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



Produced output:
['stephen.marquard at uct.ac.za', 'stephen.marquard at uct.ac.za', '
louis at media.berkeley.edu', 'louis at media.berkeley.edu', 'zqian at umich.edu', '
zqian at umich.edu', 'rjlowe at iupui.edu', 'rjlowe at iupui.edu', 'zqian at umich.edu',
'zqian at umich.edu', 'rjlowe at iupui.edu', 'rjlowe at iupui.edu', 'cwen at iupui.edu',
'cwen at iupui.edu', 'cwen at iupui.edu', 'cwen at iupui.edu', 'gsilver at umich.edu', '
gsilver at umich.edu', 'gsilver at umich.edu', 'gsilver at umich.edu', '
zqian at umich.edu', 'zqian at umich.edu', 'gsilver at umich.edu', 'gsilver at umich.edu',
'wagnermr at iupui.edu', 'wagnermr at iupui.edu', 'zqian at umich.edu', '
zqian at umich.edu', 'antranig at caret.cam.ac.uk', 'antranig at caret.cam.ac.uk', '
gopal.ramasammycook at gmail.com', 'gopal.ramasammycook at gmail.com', '
david.horwitz at uct.ac.za', 'david.horwitz at uct.ac.za', '
david.horwitz at uct.ac.za', 'david.horwitz at uct.ac.za', '
david.horwitz at uct.ac.za', 'david.horwitz at uct.ac.za', '
david.horwitz at uct.ac.za', 'david.horwitz at uct.ac.za', '
stephen.marquard at uct.ac.za', 'stephen.marquard at uct.ac.za', '
louis at media.berkeley.edu', 'louis at media.berkeley.edu', '
louis at media.berkeley.edu', 'louis at media.berkeley.edu', '
ray at media.berkeley.edu', 'ray at media.berkeley.edu', 'cwen at iupui.edu', '
cwen at iupui.edu', 'cwen at iupui.edu', 'cwen at iupui.edu', 'cwen at iupui.edu', '
cwen at iupui.edu'] ← Mismatch
There were 54 lines in the file with From as the first word


Desired output:
stephen.marquard at uct.ac.za
louis at media.berkeley.edu
zqian at umich.edu
rjlowe at iupui.edu
zqian at umich.edu
rjlowe at iupui.edu
cwen at iupui.edu
cwen at iupui.edu
gsilver at umich.edu
gsilver at umich.edu
zqian at umich.edu
gsilver at umich.edu
wagnermr at iupui.edu
zqian at umich.edu
antranig at caret.cam.ac.uk
gopal.ramasammycook at gmail.com
david.horwitz at uct.ac.za
david.horwitz at uct.ac.za
david.horwitz at uct.ac.za
david.horwitz at uct.ac.za
stephen.marquard at uct.ac.za
louis at media.berkeley.edu
louis at media.berkeley.edu
ray at media.berkeley.edu
cwen at iupui.edu
cwen at iupui.edu
cwen at iupui.edu
There were 27 lines in the file with From as the first word

Regards,
Hal







On Sun, Aug 2, 2015 at 1:18 AM, Alan Gauld <alan.gauld at btinternet.com>
wrote:

> On 02/08/15 02:20, Ltc Hotspot wrote:
>
>> Hi Alan,
>>
>> I made a mistake and incorrectly assumed that differences between 54 lines
>> of output and 27 lines of output is the result of removing duplicate email
>> addresses,
>>
>> Apparently, this is not the case and I was wrong :(
>> The solution to the problem is in the  desired line output:
>>
>> stephen.marquard at uct.ac.za
>> louis at media.berkeley.edu
>> zqian at umich.edu
>> rjlowe at iupui.edu
>> zqian at umich.edu
>> rjlowe at iupui.edu
>>
> ...
>
> OK, Only a couple of changes should see to that.
>
> Latest revised code:
>> fname = raw_input("Enter file name: ")
>> if len(fname) < 1 : fname = "mbox-short.txt"
>> fh = open(fname)
>> count = 0
>> addresses = set()
>>
>
> change this to use a list
>
> addresses = []
>
> for line in fh:
>>      if line.startswith('From'):
>>          line2 = line.strip()
>>          line3 = line2.split()
>>          line4 = line3[1]
>>          addresses.add(line4)
>>
>
> and change this to use the list append() method
>
> addresses.append(line4)
>
>          count = count + 1
>> print addresses
>> print "There were", count, "lines in the file with From as the first word"
>>
>
> I'm not quite sure where the 54/27 divergence comes from except that
> I noticed Emille mention that there were lines beginning 'From:'
> too. If that's the case then follow his advice and change the if
> test to only check for 'From ' (with the space).
>
> That should be all you need.
>
>
> --
> 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
>
>
> _______________________________________________
> 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