<div dir="ltr"><div><div><div><div><div><div><div><div><div>Colug, <br><br></div>If I have to write much more python, I will subscribe to the other lists. <br><br></div>But one more question <br><br></div>I have this snippet of borrowed code. <br>
<br>def showPayload(msg):<br> payload = msg.get_payload()<br><br> if msg.is_multipart():<br> div = ''<br> for subMsg in payload:<br> print div<br> showPayload(subMsg)<br> div = '------------------------------'<br>
else:<br> print msg.get_content_type()<br> print payload[:200]<br><br><br><br></div>I am parsing a mbox file, and the above code works. <br><br>What goes to stdout is exactly what I want. However I want to capture it to a variable. <br>
<br></div><div>This however does not work. <br><br>def showPayload2(msg):<br> message_content = ""<br> payload = msg.get_payload()<br><br> if msg.is_multipart():<br> div = ''<br> for subMsg in payload:<br>
message_content += div<br> showPayload2(subMsg)<br> div = '------------------------------'<br> else:<br> message_content += msg.get_content_type()<br> message_content += payload[:200]<br>
<br> return [message_content]<br><br><br></div><div>showPayload2 only ever returns..<br>['------------------------------']<br><br><br></div><div>Here is the code with the working function showPayload, and my attempt to capture to string instead of print to out, in showPayload2. <br>
<br></div><div>Am I missing something quick and simple to capture what is emitted to stdout to a variable ? Or how do I do a recursive function call without blowing away changes to the string ?<br><br>#!/usr/bin/python<br>
import mailbox<br><br>def showPayload(msg):<br> payload = msg.get_payload()<br><br> if msg.is_multipart():<br> div = ''<br> for subMsg in payload:<br> print div<br> showPayload(subMsg)<br>
div = '------------------------------'<br> else:<br> print msg.get_content_type()<br> print payload[:200]<br><br><br>def showPayload2(msg):<br> message_content = ""<br> payload = msg.get_payload()<br>
<br> if msg.is_multipart():<br> div = ''<br> for subMsg in payload:<br> message_content += div<br> showPayload2(subMsg)<br> div = '------------------------------'<br>
else:<br> message_content += msg.get_content_type()<br> message_content += payload[:200]<br> <br> return [message_content] <br><br>mbox = mailbox.mbox('201308.mbox')<br>for message in mbox:<br>
data = {}<br> data['From'] = message['from']<br> data['Subject'] = message['subject']<br> data['Date'] = message['date']<br> data['Content'] = showPayload2(message)<br>
print data<br> showPayload(message)<br> print showPayload2(message)<br></div></div></div></div></div><br><br><div><div><div><div><div><div><div><br><div><br><br></div></div></div></div></div></div></div></div></div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Aug 1, 2013 at 9:13 PM, <span dir="ltr"><<a href="mailto:jep200404@columbus.rr.com" target="_blank">jep200404@columbus.rr.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Thu, 1 Aug 2013 15:13:07 -0400, Tom Hanlon <<a href="mailto:tom@functionalmedia.com">tom@functionalmedia.com</a>> wrote:<br>
<br>
> That is sweet !!<br>
><br>
> Python is nice,<br>
><br>
> I think what I just did was<br>
><br>
> #!/usr/bin/python<br>
> from colug import experience<br>
<br>
</div>There's a local Python group and mailing list at<br>
<a href="http://cohpy.org/" target="_blank">http://cohpy.org/</a> and<br>
<a href="http://mail.python.org/mailman/listinfo/centraloh" target="_blank">http://mail.python.org/mailman/listinfo/centraloh</a><br>
where other bits of Python niftiness occurs.<br>
<br>
<a href="http://mail.python.org/pipermail/centraloh/2013-June/001718.html" target="_blank">http://mail.python.org/pipermail/centraloh/2013-June/001718.html</a><br>
<br>
You missed PyOhio, an annual free Python conference held<br>
at the Ohio Union on OSU campus, but you can watch the videos.<br>
<br>
<a href="http://pyvideo.org/category/41/pyohio-2013" target="_blank">http://pyvideo.org/category/41/pyohio-2013</a><br>
<br>
You should check out IPython Notebook.<br>
<br>
<a href="http://pyvideo.org/video/1605/science-and-python-retrospective-of-a-mostly-s" target="_blank">http://pyvideo.org/video/1605/science-and-python-retrospective-of-a-mostly-s</a><br>
<a href="http://pyvideo.org/video/2286/the-ipython-notebook-revolution" target="_blank">http://pyvideo.org/video/2286/the-ipython-notebook-revolution</a><br>
<a href="http://mail.python.org/pipermail/centraloh/2013-February/001551.html" target="_blank">http://mail.python.org/pipermail/centraloh/2013-February/001551.html</a><br>
<div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
colug-432 mailing list<br>
<a href="mailto:colug-432@colug.net">colug-432@colug.net</a><br>
<a href="http://lists.colug.net/mailman/listinfo/colug-432" target="_blank">http://lists.colug.net/mailman/listinfo/colug-432</a><br>
</div></div></blockquote></div><br></div>