[Tutor] Trouble with encoding/decoding a file

ALAN GAULD alan.gauld at btinternet.com
Mon Feb 6 20:02:13 CET 2012


That's not too surprising. Text files use an end-of-file character combination. 
You might be lucky and not encounter that set of bytes in your file or you 
might be unlucky and find it, in which case the file gets truncated. 
That's why you need the binary setting, to ensure you read the 
whole file!
 
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/



>________________________________
> From: Tony Pelletier <tony.pelletier at gmail.com>
>To: Alan Gauld <alan.gauld at btinternet.com> 
>Cc: tutor at python.org 
>Sent: Monday, 6 February 2012, 18:40
>Subject: Re: [Tutor] Trouble with encoding/decoding a file
> 
>
>
>
>
>On Mon, Feb 6, 2012 at 1:13 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>On 06/02/12 15:11, Tony Pelletier wrote:
>>
>>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')
>>>
>>
>>
>>Have you tried opening as a binary file?
>>>Excel .xls uses binary data...
>
>
>Unbelievable.  In the snippet you quoted above, it didn't matter.  That one always worked, but changing it in the code I wrote, it totally fixed it.  :)
>
>
>Thanks! 
>
>>-- 
>>Alan G
>>Author of the Learn to Program web site
>>http://www.alan-g.me.uk/
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>To unsubscribe or change subscription options:
>>http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120206/7420a7dc/attachment-0001.html>


More information about the Tutor mailing list