[Tutor] AttributeError,
Steven D'Aprano
steve at pearwood.info
Tue Aug 18 03:04:57 CEST 2015
Has anyone else answered this question? I haven't seen a response, but
perhaps I missed it.
More comments below.
On Tue, Aug 11, 2015 at 09:53:03PM -0700, Ltc Hotspot wrote:
> Hi Steven,
>
> Message heard loud and clear:
>
> Question: What sorted function should I write to produce the desired
> output, below:
>
> Desired output:
>
> 04 3
> 06 1
> 07 1
> 09 2
> 10 3
> 11 6
> 14 1
> 15 2
> 16 4
> 17 2
> 18 1
> 19 1
You do a good job of showing your desired output, but not the input. The
simplest possible code that will output the above string is:
print("""04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1
""")
but naturally if the input is something else, you will need to do more
work. Without knowing what the input is, there is no way to know what
work you need to do to it.
> Latest revised code:
>
> count = dict()
> fname = raw_input("Enter file name: ")#
> handle = open (fname, 'r')#
> for line in handle:
> if line.startswith("From "):
> address = line.split()[5]
Are you sure that's giving you the address? Given the output you show
below, the "address" looks like this:
11:11:52
which looks to me like you are splitting on a date/time, not an email
adress.
> line = line.rstrip()
The above line is useless. You don't do anything with line after this
point, so why bother?
> count[address] = count.get(address, 0) + 1
>
> lst = list()
> for key,val in count.items():
> lst.append( (val, key) )
> lst.sort(reverse=True)
> for val, key in lst[:12]:
> print key,val
>
>
> Output code:
>
> In [3]: %run assignment_10_2_v_01
> Enter file name: mbox-short.txt
> 16:23:48 1
> 16:23:48 1
> 11:11:52 1
> 17:07:00 1
> 16:23:48 1
[snip]
Please don't show hundreds of lines of output. Reduce it to a few lines,
maybe a dozen at most. And again, without seeing the input, how are we
supposed to tell whether the output is wrong or not?
And, no, please don't attach a mbox file to your email. Construct your
own SHORT input data:
data = [
'From abc at xyz\n',
'line of text\n',
'From foo at bar.com\n',
'more text\n',
'more text\n',
'From abc at xyz \n',
'more text \n',
'From Abc at xyz\n',
]
Then operate on the fake data:
for line in data:
...
But I strongly expect that the problem is that your data doesn't look
like what you think it looks like. The format of the "From " line in
mbox looks like this:
"From address day month date time year"
so when you split it and take the 5th element, you get the time, not the
address.
--
Steve
More information about the Tutor
mailing list