Reputation:
I'm creating a mail "bot" for one of my web services that will periodically collect a queue of e-mail messages to be sent from a PHP script and send them via Google's SMTP servers. The PHP script returns the messages in this format:
[email protected]:Full Name:shortname\[email protected]:Another Full Name:anothershortname\[email protected]:Foo:bar
I need to "convert" that into something like this:
{
"[email protected]": [
[
"Full Name",
"shortname"
],
[
"Foo",
"bar"
]
],
"[email protected]": [
[
"Another Full Name",
"anothershortname"
]
]
}
Notice I need to have only one key per e-mail, even if there are multiple instances of an address. I know I can probably do it with two consecutive loops, one to build the first level of the dictionary and the second to populate it, but there should be a way to do it in one shot. This is my code so far:
raw = "[email protected]:Full Name:shortname\[email protected]:Another Full Name:anothershortname\[email protected]:Foo:bar"
print raw
newlines = raw.split("\n")
print newlines
merged = {}
for message in newlines:
message = message.split(":")
merged[message[0]].append([message[1], message[2]])
print merged
I'm getting a KeyError on the last line of the loop, which I take to mean the key has to exist before appending anything to it (appending to a nonexistent key will not create that key).
I'm new to Python and not really familiar with lists and dictionaries yet, so your help is much appreciated!
Upvotes: 2
Views: 2642
Reputation: 29302
I see that you've already accepted an answer, but maybe you're anyhow interested that what you're doing can be easily achieved with defaultdict
:
from collections import defaultdict
raw = "[email protected]:Full Name:shortname\[email protected]:Another Full Name:anothershortname\[email protected]:Foo:bar"
merged = defaultdict(list)
for line in raw.split('\n'):
line = line.split(':')
merged[line[0]].append(line[1:])
Upvotes: 1
Reputation: 5918
You are right about the error. So you have to check if the key is present. 'key' in dict
returns True
if 'key'
is found in dict
, otherwise False
. Implementing this, here's your full code (with the debugging print statements removed):
raw = "[email protected]:Full Name:shortname\[email protected]:Another Full Name:anothershortname\[email protected]:Foo:bar"
newlines = raw.split("\n")
merged = {}
for message in newlines:
message = message.split(":")
if message[0] in merged:
merged[message[0]].append([message[1], message[2]])
else:
merged[message[0]]=[[message[1], message[2]]]
print merged
Notice the extra brackets for the nested list on the second last line.
Upvotes: 0
Reputation: 43235
Just check for presence of key, if it is not present, create the key, if it is present, then append the data to existing list.
if(messsage[0] in merged):
merged[message[0]] = [message[1],[message[2]]
else:
merged[message[0]].append([[message[1], message[2]])
Upvotes: 0
Reputation: 2460
May work as:
for message in newlines:
message = message.split(":")
temp = []
temp.append(message[1])
temp.append(message[2])
merged[message[0]] = temp
Actually maybe:
for message in newlines:
message = message.split(":")
temp = []
temp.append(message[1])
temp.append(message[2])
if message[0] not in merged:
merged[message[0]] = []
merged[message[0]].append(temp)
Upvotes: 1