[Tutor] dictionary manipulation
Bob Gailer
bgailer at alum.rpi.edu
Wed Jul 26 19:24:10 CEST 2006
Chris Hallman wrote:
>
> I need some suggestions on how to work with a dictionary. I've got a
> program that builds a dictionary. I need to be able to manipulate the
> different keys and data so that I can write the output to a file AND
> utilize the smtplib to send the data in an email. I had problems using
> the data in the dictionary, so I wrote a crude for loop to strip
> characters:
>
> >>> print result
> {'s0100swa': ['running correct IOS'], 's0300swa': ['INCORRECT running
> IOS'], 's0200swa': ['running correct IOS'], 's0400swa': ['running
> correct IOS']}
>
> results= sorted(result.items ())
>
> >>> print results
> [('s0100swa', ['running correct IOS']), ('s0200swa', ['running correct
> IOS']), ('s0300swa', ['INCORRECT running IOS']), ('s0400swa',
> ['running correct IOS'])]
>
> for x in range(len(results)):
> text = repr(results[x])
> text = text.replace("'", "")
> text = text.replace("(", "")
> text = text.replace(")", "")
> text = text.replace ("[", "")
> text = text.replace("]", "")
> text = text.replace(",", ":")
> output.write(text + "\n")
> output.flush()
> output.write ("\n")
>
> I need to remove the extra characters so the file and email output
> isn't difficult to read. I also need to be able to use the dictionary
> data so that I can compose the message used by the smtplib.
>
>
> Any suggestions?
I for one have no suggestions to offer because I don't know what you
want. "suggestions on how to work with a dictionary" is not adequate.
Where are you stuck? Do you want critique of the code you posted, or how
use smtplib or what?
--
Bob Gailer
510-978-4454
More information about the Tutor
mailing list