Hi Alan,
There is an indent message in the revised code.
Question: Where should I indent the code line for the loop?
View the revised codes with loop indents, below:
--->Revised Code v.2 wo/indent from lines 8-12:
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:
if line.startswith('From'):
line2 = line.strip()
line3 = line2.split()
line4 = line3[1]
addresses.add(line)
count = count + 1
print "There were", count, "lines in the file with From as the first word"
print addresses
---> Message output reads:
In [62]: %run _8_5_v_25.py
File "C:\Users\vm\Desktop\apps\docs\Python\_8_5_v_25.py", line 8
line2 = line.strip()
^
IndentationError: expected an indented block
--->Revised Code v.3 w/indent from lines 8-12:
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:
if line.startswith('From'):
line2 = line.strip()
line3 = line2.split()
line4 = line3[1]
addresses.add(line)
count = count + 1
print "There were", count, "lines in the file with From as the first word"
print addresses
---> Message output reads:
...pi/component/src/java/org/sakaiproject/component/util/RecordWriter.java\n',
'Dat
e: 2008-01-04 11:09:12 -0500 (Fri, 04 Jan 2008)\n', '\t 4 Jan 2008 11:12:30
-050
0\n', '\tby nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11/Submit) id
m03M5Ea
7005273\n', 'New Revision: 39755\n', 'X-DSPAM-Processed: Thu Jan 3
16:23:48 200
8\n', 'Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39754\n',
'
\t Fri, 04 Jan 2008 11:35:08 -0500\n', '\tfor <
***@collab.sakaiproject.org>;
Fri, 4 Jan 2008 04:05:54 -0500\n', 'Received: from carrie.mr.itd.umich.edu
(carr
ie.mr.itd.umich.edu [141.211.93.152])\n', 'Message-ID:
<200801042044.m04Kiem3007
***@nakamura.uits.iupui.edu>\n', '\tfor <***@collab.sakaiproject.org>;
Fri,
4 Jan 2008 16:36:37 +0000 (GMT)\n', '\t Fri, 04 Jan 2008 15:03:18 -0500\n',
'\tF
ri, 4 Jan 2008 16:11:31 +0000 (GMT)\n', ' by paploo.uhi.ac.uk
(JAMES S
MTP Server 2.1.3) with SMTP ID 960\n', 'From ***@media.berkeley.edu Fri
Jan 4
18:10:48 2008\n', ' Thu, 3 Jan 2008 22:06:34 +0000 (GMT)\n',
'\tfor so
***@collab.sakaiproject.org; Fri, 4 Jan 2008 10:15:57 -0500\n', 'Received:
from
eyewitness.mr.itd.umich.edu (eyewitness.mr.itd.umich.edu
[141.211.93.142])\n',
'Subject: [sakai] svn commit: r39743 -
gradebook/branches/oncourse_2-4-2/app/ui/
src/java/org/sakaiproject/tool/gradebook/ui\n', 'Date: 2008-01-04 10:15:54
-0500
(Fri, 04 Jan 2008)\n', 'New Revision: 39761\n', '\tBY
salemslot.mr.itd.umich.ed
u ID 477DF74E.49493.30415 ; \n', 'X-DSPAM-Processed: Sat Jan 5 09:14:16
2008\n'
, '\tfor <***@collab.sakaiproject.org>; Fri, 4 Jan 2008 21:10:14 +0000
(GMT)
\n', '\tby paploo.uhi.ac.uk (Postfix) with ESMTP id 88598BA5B6;\n',
'X-DSPAM-Pro
cessed: Fri Jan 4 04:07:34 2008\n', 'r39558 | ***@iupui.edu | 2007-12-20
15:25:
38 -0500 (Thu, 20 Dec 2007) | 3 lines\n', 'From ***@umich.edu Fri Jan
4 11:
10:22 2008\n', '\tby nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11)
with ESM
TP id m04N8vHG008127\n', '\tSat, 5 Jan 2008 14:10:05 +0000 (GMT)\n', '\tby
naka
mura.uits.iupui.edu (8.12.11.20060308/8.12.11/Submit) id m049W2i5006493\n',
'\tT
hu, 3 Jan 2008 22:06:57 +0000 (GMT)\n', ' Fri, 4 Jan 2008
19:46:50 +00
00 (GMT)\n', 'Message-ID: <
***@nakamura.uits.iupui.edu>\
n', 'Subject: [sakai] svn commit: r39756 - in
component/branches/SAK-12166/compo
nent-api/component/src/java/org/sakaiproject/component: impl
impl/spring/support
impl/spring/support/dynamic impl/support util\n',
'site/trunk/site-tool/tool/sr
c/bundle/admin.properties\n', 'Author: ***@gmail.com\n',
'From d
***@uct.ac.za Fri Jan 4 04:33:44 2008\n', '\tby
nakamura.uits.iupui.ed
u (8.12.11.20060308/8.12.11) with ESMTP id m04E3pQS006928\n', '\tfor
***@coll
ab.sakaiproject.org; Fri, 4 Jan 2008 16:09:02 -0500\n', 'X-DSPAM-Processed:
Fri
Jan 4 09:05:31 2008\n', '\t 4 Jan 2008 16:10:33 -0500\n', '\tfor
***@collab.
sakaiproject.org; Fri, 4 Jan 2008 11:09:14 -0500\n', 'merge fix to SAK-9996
into
2-5-x branch: svn merge -r 39687:39688
https://source.sakaiproject.org/svn/site
-manage/trunk/\n', 'Subject: [sakai] svn commit: r39751 - in
podcasts/branches/s
akai_2-5-x/podcasts-app/src/webapp: css images podcasts\n', 'Subject:
[sakai] sv
n commit: r39757 - in assignment/trunk:
assignment-impl/impl/src/java/org/sakaip
roject/assignment/impl assignment-tool/tool/src/webapp/vm/assignment\n',
'From w
***@iupui.edu Fri Jan 4 10:38:42 2008\n', 'Date: 2008-01-03 17:16:39
-0500
(Thu, 03 Jan 2008)\n', ' by paploo.uhi.ac.uk (JAMES SMTP Server
2.1.3)
with SMTP ID 906\n', 'U
podcasts/podcasts-app/src/webapp/podcasts/podOptions.
jsp\n', 'svn merge -c 35014
https://source.sakaiproject.org/svn/gradebook/trunk\
n', 'Received: from galaxyquest.mr.itd.umich.edu (
galaxyquest.mr.itd.umich.edu [
141.211.93.145])\n', '\tBY salemslot.mr.itd.umich.edu ID
477D5F23.797F6.16348 ;
\n', 'Date: Fri, 4 Jan 2008 18:08:57 -0500\n', 'X-DSPAM-Processed: Fri Jan
4 04
:33:44 2008\n',
'polls/trunk/tool/src/java/org/sakaiproject/poll/tool/evolvers/\
n', 'Date: 2008-01-04 10:01:40 -0500 (Fri, 04 Jan 2008)\n',
'X-DSPAM-Confidence:
0.8475\n', '\tFri, 4 Jan 2008 09:48:55 +0000 (GMT)\n',
'X-DSPAM-Processed: Fri
Jan 4 06:08:27 2008\n', '\tBY anniehall.mr.itd.umich.edu ID
477D5C7A.4FE1F.222
11 ; \n', '\t Fri, 04 Jan 2008 11:10:22 -0500\n', '\tBY
workinggirl.mr.itd.umich
.edu ID 477DFD6C.75DBE.26054 ; \n', 'svn log -r 39403
https://source.sakaiprojec
t.org/svn/gradebook/trunk\n', '\t Thu, 03 Jan 2008 19:51:21 -0500\n',
'Date: Fri
, 4 Jan 2008 11:08:39 -0500\n', '\tby shmi.uhi.ac.uk (Postfix) with ESMTP
id C59
6A3DFA2\n', '\tby shmi.uhi.ac.uk (Postfix) with ESMTP id 8889842C49\n',
'X-DSPAM
-Processed: Fri Jan 4 11:12:37 2008\n', 'Details:
http://source.sakaiproject.or
g/viewsvn/?view=rev&rev=39772\n', '\tby shmi.uhi.ac.uk (Postfix) with ESMTP
id 8
C13342C92\n', 'Date: Fri, 4 Jan 2008 11:09:14 -0500\n', '\tFri, 4 Jan 2008
10:17
:42 -0500\n', 'New Revision: 39754\n', 'New Revision: 39749\n', 'Details:
http:/
/source.sakaiproject.org/viewsvn/?view=rev&rev=39755\n', 'svn merge -c
39403 htt
ps://source.sakaiproject.org/svn/gradebook/trunk\n', ' by
paploo.uhi.ac
.uk (JAMES SMTP Server 2.1.3) with SMTP ID 385\n',
'site-manage/branches/sakai_2
-4-x/site-manage-tool/tool/src/java/org/sakaiproject/site/tool/SiteAction.java\n
', 'X-DSPAM-Confidence: 0.6178\n', '\t Fri, 04 Jan 2008 11:12:37 -0500\n',
'Deta
ils: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39765\n',
'Content-Typ
e: text/plain; charset=UTF-8\n', ' Fri, 4 Jan 2008 09:07:04 +0000
(GMT)
\n', 'Date: Fri, 4 Jan 2008 09:03:51 -0500\n', 'From ***@iupui.edu Fri
Jan 4 1
1:35:08 2008\n', '\tby nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11)
with E
SMTP id m049lU3P006519\n', 'r35014 | ***@iupui.edu | 2007-09-12
16:17:59 -0
400 (Wed, 12 Sep 2007) | 3 lines\n',
'component/branches/SAK-12166/component-api
/component/src/java/org/sakaiproject/component/impl/support/DynamicComponentReco
rd.java\n',
'sam/branches/SAK-12065/samigo-app/src/java/org/sakaiproject/tool/as
sessment/ui/bean/evaluation/QuestionScoresBean.java\n', '\tby
paploo.uhi.ac.uk (
Postfix) with ESMTP id DEC65ADC79;\n', '\tfor <
***@collab.sakaiproject.org>;
Fri, 4 Jan 2008 00:25:00 +0000 (GMT)\n', "Sakai Source Repository
\t#38024 \t
Wed Nov 07 14:54:46 MST 2007 \***@umich.edu \t Fix to SAK-10788: If a
provi
ded id in a couse site is fake or doesn't provide any user information,
Site Inf
o appears to be like project site with empty participant list\n", 'Watch
for enr
ollments object being null and concatenate provider ids when there are more
than
one.\n',
'gradebook/trunk/app/ui/src/java/org/sakaiproject/tool/gradebook/ui/he
lpers/params/GradeGradebookItemViewParams.java\n', 'Date: Fri, 4 Jan 2008
16:09:
02 -0500\n', '\tfor <***@collab.sakaiproject.org>; Thu, 3 Jan 2008
21:28:38
+0000 (GMT)\n', 'Details:
http://source.sakaiproject.org/viewsvn/?view=rev&rev=3
9742\n', '\tFri, 4 Jan 2008 14:50:17 -0500\n', 'Date: Fri, 4 Jan 2008
04:05:53 -
0500\n', '\tby paploo.uhi.ac.uk (Postfix) with ESMTP id 6A39594CD2;\n',
'From da
***@uct.ac.za Fri Jan 4 06:08:27 2008\n', 'Subject: [sakai] svn
commit:
r39750 -
event/branches/SAK-6216/event-util/util/src/java/org/sakaiproject/util
\n', 'SAK-9882: refactored the other pages as well to take advantage of
proper j
sp components as well as validation cleanup.\n', '\tfor
<***@collab.sakaiproj
ect.org>; Fri, 4 Jan 2008 11:33:06 -0500\n', 'Received: from
shining.mr.itd.umic
h.edu (shining.mr.itd.umich.edu [141.211.93.153])\n', 'Message-ID:
<200801042109
***@nakamura.uits.iupui.edu>\n', 'Return-Path: <
***@collab.sa
kaiproject.org>\n', 'polls/branches/sakai_2-5-x/.classpath\n',
'X-DSPAM-Processe
d: Fri Jan 4 11:37:30 2008\n', '\tFri, 4 Jan 2008 06:08:26 -0500\n', '\tby
shmi
.uhi.ac.uk (Postfix) with ESMTP id 7D13042F71\n', ' Fri, 4 Jan
2008 14:
05:04 +0000 (GMT)\n', 'X-Authentication-Warning: nakamura.uits.iupui.edu:
apache
set sender to ***@caret.cam.ac.uk using -f\n',
'gradebook/trunk/service/ap
i/src/java/org/sakaiproject/service/gradebook/shared/GradebookService.java\n',
'
Subject: [sakai] svn commit: r39753 - in polls/trunk: . tool
tool/src/java/org/s
akaiproject/poll/tool tool/src/java/org/sakaiproject/poll/tool/evolvers
tool/src
/webapp/WEB-INF\n', '\tby nakamura.uits.iupui.edu
(8.12.11.20060308/8.12.11) wit
h ESMTP id m04F21hn007033\n', '\tBY galaxyquest.mr.itd.umich.edu ID
477D5397.E16
1D.20326 ; \n', ' ...
Regards,
Hal
On Sat, Aug 1, 2015 at 1:14 AM, Alan Gauld <***@btinternet.com>
wrote:
> On 01/08/15 00:59, ***@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', '***@collab.sakaiproject.org;', 'Jan', 'mail.umich.edu',
>> 'Innocen
>> t', '0.0000', 'CMU', 'frankenstein.mail.umich.edu', '0.8475', 'from',
>> '***@co
>> llab.sakaiproject.org', '05',
>> '<***@nakamura.uits.iupui.
>> edu>', 'flawless.mail.umich.edu', '5', 'nakamura.uits.iupui.edu:', '
>> shmi.uhi.ac.
>> uk', '7bit', 'text/plain;', '<***@collab.sakaiproject.org>;', 'Sat,',
>> 'nakamu
>> ra.uits.iupui.edu', 'paploo.uhi.ac.uk', 'FROM', 'holes.mr.itd.umich.edu',
>> '(from
>> ', '<***@collab.sakaiproject.org>', '[sakai]',
>> '***@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
>
>
> _______________________________________________
> Tutor maillist - ***@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/li