Discussion:
[Tutor] Mailbox
l***@gmail.com
2015-07-29 21:55:34 UTC
Permalink
Hi Everyone:






I have a second and unrelated question:





I tried to work backward to see if there is a logic error associated with a variable is being skipped over:




#top of code, initialize variable
output_list = ["default"]




#rest of code
If you get at the end





print output_list





['default']





Raw Data File:




count = 0
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
for line in fname:
line = line.strip()
if not line.startswith('From '): continue
line = line.split()
count = count + 1
print len(line)
fh = open(fname)
print "There were", count, "lines in the file with From as the first word"





Sample data file at http://www.pythonlearn.com/code/mbox-short.txt





Desired Output:




***@uct.ac.za
***@media.berkeley.edu
***@umich.edu
***@iupui.edu
***@umich.edu
***@iupui.edu





Thanks,

Hal


Sent from Surface





From: Phu Sam
Sent: ‎Wednesday‎, ‎July‎ ‎29‎, ‎2015 ‎1‎:‎06‎ ‎PM
To: Ltc Hotspot





































_______________________________________________
Baypiggies mailing list
***@python.org
To change your subscription options or unsubscribe:
https://mail.python.org/mailman/listinfo/baypiggies
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription opt
Cameron Simpson
2015-07-30 02:01:05 UTC
Permalink
On 29Jul2015 21:55, ***@gmail.com <***@gmail.com> wrote:
>I have a second and unrelated question:
>
>I tried to work backward to see if there is a logic error associated with a variable is being skipped over:
>
>#top of code, initialize variable
>output_list = ["default"]

Curious: why not just make this an empty list: []

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

My first observation would be that "count = count + 1" is not inside the loop.
That means that it fires just once, _after_ the loop. So it will always be 1.

Also, what is the purpose of this line:

line = line.split()

Cheers,
Cameron Simpson <***@zip.com.au>
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-07-30 07:26:21 UTC
Permalink
On 29/07/15 22:55, ***@gmail.com wrote:

> #top of code, initialize variable
> output_list = ["default"]
>
> #rest of code
> print output_list
> ['default']
>
> Raw Data File:

Note, this is the code not the data...

> count = 0
> fname = raw_input("Enter file name: ")
> if len(fname) < 1 : fname = "mbox-short.txt"
> for line in fname:

You are still looping over the filename not the file contents.
thus line will take the values: m, b, ,o, x -, s, h,....

> line = line.strip()
> if not line.startswith('From '): continue

And this will always be true so the loop will stop here.

> line = line.split()
> count = count + 1

this is outside the loop so will always be 1

> print len(line)
> fh = open(fname)

this opens the file but you do nothing with it.

> print "There were", count, "lines in the file with From as the first word"

You do not seem to have changed anything from your
original post. All the same errors are still there.

--
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...