[Tutor] Trouble with encoding/decoding a file

Tony Pelletier tony.pelletier at gmail.com
Mon Feb 6 16:11:12 CET 2012


Hi,

I've been tasked with getting the encoded value using a SOAP call and then
writing the file out.  I first used the interpreter to do so like such:

import base64

encoded = 'super long encoded string'
data = base64.b64decode(encoded)
outfile = open('test.xls', 'w')
outfile.write(data)
outfile.close

And that all worked just fine.  The difference here is I am cutting and
pasting the encoded value into a string from notepad++.

So, I then did the following which I can't get working.  It keeps telling
me "excel found unreadable content in the file' essentially being corrupt
and I can't tell what I'm doing wrong.

try:
eventresult = service.client.service.EventQueryById(event, 'true')
filename = eventresult.Event[0].EventFileArgs.EventFileArg[0].UserFileName
encodedfile =
str(eventresult.Event[0].EventFileArgs.EventFileArg[0].EncodedValue)
encodedstring = encodedfile.encode('utf-8')
str_list = []
for line in encodedstring:
line = line.strip()
str_list.append(line)
string = ''.join(str_list)
print string
data = base64.b64decode(string)
txtfile = open('trp.txt', 'w')
txtfile.write(string)
outfile = open(filename, 'w')
outfile.write(data)
outfile.close()
except suds.WebFault as e:
print e.fault.detail

The txt file is only there to make sure I'm writing out one long string
which is what I'm pasting in so I'm assuming that's correct.  Any help is
greatly appreciated.

Thanks
Tony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120206/eee289fc/attachment.html>


More information about the Tutor mailing list