[Tutor] String Attribute
Alan Gauld
alan.gauld at btinternet.com
Sun Aug 2 16:06:56 CEST 2015
On 02/08/15 10:15, Ltc Hotspot wrote:
> Question1: Why did the following strip function fail: line2 =
> line.strip (',')
What makes you think it failed?
I see no error messages below.
> Question2: How do I code a vertical column output
See below.
> 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'):
You are still not checking for 'From ' - with a space. Thats why you still
get 54 instead of 27.
> line2 = line.strip ()
> line3 = line2.split()
> line4 = line3[1]
> addresses.append(line4)
> count = count + 1
> print addresses
To get a vertical printout try this:
print '\n'.join(addresses)
Which converts the list into a string with a newline between each element.
Alternatively do it the manual way:
for addr in addresses: print addr
--
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