Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44495

Re: How do I encode and decode this data to write to a file?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'static': 0.04; '(using': 0.07; 'debug': 0.07; 'debugging': 0.07; 'encoded': 0.07; 'failing': 0.07; 'intermediate': 0.07; 'skip:" 60': 0.07; 'subject:file': 0.07; 'tries': 0.07; 'utf-8': 0.07; 'ascii': 0.09; 'encode': 0.09; 'forcing': 0.09; 'mess': 0.09; 'none)': 0.09; 'omit': 0.09; 'subject:How': 0.10; 'python': 0.11; '2.7': 0.14; 'creates': 0.14; '"w")': 0.16; '(should': 0.16; '238,': 0.16; 'chasing': 0.16; 'codec': 0.16; 'comments:': 0.16; 'file.close()': 0.16; 'hierarchy': 0.16; 'letting': 0.16; 'name).': 0.16; 'ordinal': 0.16; 'statements,': 0.16; 'url:py': 0.16; 'elements': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'basically': 0.19; 'split': 0.19; 'things.': 0.19; 'not,': 0.20; 'saying': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'byte': 0.24; 'bytes': 0.24; 'skip': 0.24; 'unicode': 0.24; 'file.': 0.24; 'source': 0.25; 'mention': 0.26; 'skip:" 30': 0.26; 'second': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'character': 0.29; 'characters': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; 'apparently': 0.31; 'file': 0.32; 'there.': 0.32; 'run': 0.32; 'linux': 0.33; 'url:python': 0.33; 'running': 0.33; '(most': 0.33; 'raw': 0.33; 'problem': 0.35; "can't": 0.35; 'advice': 0.35; 'created': 0.35; 'but': 0.35; 'version': 0.36; 'really': 0.36; 'data,': 0.36; 'gallery': 0.36; 'skip:f 40': 0.36; 'subject:data': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'effort': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'generic': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'recent': 0.39; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'skip:o 30': 0.61; 'full': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; 'url:0': 0.67; 'line,': 0.68; 'received:74.208': 0.68; 'containing': 0.69; 'incoming': 0.72; 'walk': 0.74; 'subject:this': 0.83; 'presumably': 0.84; 'received:74.208.4.194': 0.84; 'examine': 0.93
Date Mon, 29 Apr 2013 07:46:51 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5
MIME-Version 1.0
To python-list@python.org
Subject Re: How do I encode and decode this data to write to a file?
References <27s15a-943.ln1@chris.zbmc.eu>
In-Reply-To <27s15a-943.ln1@chris.zbmc.eu>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Provags-ID V02:K0:KoFdOhHwDUCGm/doFSOiKCrTLIs0GUW7dpzzBENuvZf ETBwIOtYeUGDu5bRP/SBV30jt9uVvi+KCCGIbnusm3zaNAXt+B q5kKbKPZGbBYTY0tLDoSrrBEZU8UhudVn8Gt37aXFnx9tMRjh3 yJSrJHAci6UBvP1PUGBEaQToRvG84PpC2MZ5ZBuXWDz6LiEjOM 9O7ggI3M3uRJ5NmRWFL1ZaA25djkeT2JbvE9Kxp+ZMQppp2jLa 0+ujkJ9WesbUNMcMCLxgGX1kSBwuuwFQQak5d/y4qXkf7oGtAi oX5lZXa2Igr+K//bw64qS0VHDmLyO3QC3WnVLM9kepI8XJKxA= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1151.1367236032.3114.python-list@python.org> (permalink)
Lines 78
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1367236032 news.xs4all.nl 15926 [2001:888:2000:d::a6]:50012
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:44495

Show key headers only | View raw


On 04/29/2013 05:47 AM, cl@isbd.net wrote:

A couple of generic comments:  your email program made a mess of the 
traceback by appending each source line to the location information.

Please mention your Python version & OS.  Apparently you're running 2.7 
on Linux or similar.

> I am debugging some code that creates a static HTML gallery from a
> directory hierarchy full of images. It's this package:-
>      https://pypi.python.org/pypi/Gallery2.py/2.0
>
>
> It's basically working and does pretty much what I want so I'm happy to
> put some effort into it and fix things.
>
> The problem I'm currently chasing is that it can't cope with directory
> names that have accented characters in them, it fails when it tries to
> write the HTML that creates the page with the thumbnails on.
>
> The code that's failing is:-
>
>          raw = os.path.join(directory, self.getNameNoExtension()) + ".html"
>          file = open(raw, "w")
>          file.write("".join(html).encode('utf-8'))

You can't encode byte data, it's already encoded. So you're forcing the 
Python system to implicitly decode it (using ASCII codec) before letting 
you encode it to utf-8.  If you think it's already in utf-8, then omit 
the encode() call there.

Additionally, you can debug things with some simple print statements, at 
least if you decompose your 3-function line so you can get at the 
intermediate data.  Split the line into three parts;
     temp1 = "".join(html)     #temp1 is byte data
     temp2 = temp1.decode()    #temp2 is unicode data
     temp3 = temp2.encode("utf-8")  #temp3 is byte data again
     file.write(temp3)

Now, you'll presumably get the error on the second line, so examine the 
bytes around byte 783.  Make sure it's really in utf-8, and if it is, 
then skip the decode and the encode.  If it's not, then Andrew's advice 
is pertinent.

I would also look at the variable html.  It's a list, but what are the 
types of the elements in it?

>          file.close()
>
> The variable html is a list containing the lines of HTML to write to the
> file.  It fails when it contains accented characters (an é in this
> case).  Here's the traceback:-
>
> Traceback (most recent call last):
>    File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 41, in run self._recurse()
>    File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 272, in _recurse os.path.walk(self.props["sourcedir"], self.processDir, None)
>    File "/usr/lib/python2.7/posixpath.py", line 246, in walk walk(name, func, arg) File "/usr/lib/python2.7/posixpath.py", line 246, in walk walk(name, func, arg)
>    File "/usr/lib/python2.7/posixpath.py", line 246, in walk walk(name, func, arg) File "/usr/lib/python2.7/posixpath.py", line 238, in walk func(arg, top, names)
>    File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 263, in processDir self.createGallery()
>    File "/usr/local/lib/python2.7/dist-packages/gallery/galleries.py", line 215, in createGallery self.picturemanager.createPictureHTMLs(self.footer)
>    File "/usr/local/lib/python2.7/dist-packages/gallery/picturemanager.py", line 84, in createPictureHTMLs curPic.createPictureHTML(self.galleryDirectory, self.getStylesheet(), self.fullsize, footer)
>    File "/usr/local/lib/python2.7/dist-packages/gallery/picture.py", line 361, in createPictureHTML file.write("".join(html).encode('utf-8')) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 783: ordinal not in range(128)
>
>
>
> If I understand correctly the encode() is saying that it can't
> understand the data in the html because there's a character 0xc3 in it.
> I *think* this means that the é is encoded in UTF-8 already in the
> incoming data stream (should be as my system is wholly UTF-8 as far as I
> know and I created the directory name).
>
> So how do I change the code so I don't get the error?  Do I just
> decode() the data first and then encode() it?
>


-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How do I encode and decode this data to write to a file? cl@isbd.net - 2013-04-29 10:47 +0100
  Re: How do I encode and decode this data to write to a file? Andrew Berg <bahamutzero8825@gmail.com> - 2013-04-29 05:11 -0500
    Re: How do I encode and decode this data to write to a file? cl@isbd.net - 2013-04-29 13:50 +0100
  Re: How do I encode and decode this data to write to a file? Peter Otten <__peter__@web.de> - 2013-04-29 12:33 +0200
  Re: How do I encode and decode this data to write to a file? Dave Angel <davea@davea.name> - 2013-04-29 07:46 -0400
    Re: How do I encode and decode this data to write to a file? cl@isbd.net - 2013-04-29 13:59 +0100
      Re: How do I encode and decode this data to write to a file? Robert Kern <robert.kern@gmail.com> - 2013-04-29 14:11 +0100
        Re: How do I encode and decode this data to write to a file? cl@isbd.net - 2013-04-29 15:38 +0100
          Re: How do I encode and decode this data to write to a file? Skip Montanaro <skip@pobox.com> - 2013-04-29 09:56 -0500
  Re: How do I encode and decode this data to write to a file? Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-29 18:02 -0400
  Re: How do I encode and decode this data to write to a file? Tony the Tiger <tony@tiger.invalid> - 2013-05-01 16:20 -0500
    Re: How do I encode and decode this data to write to a file? Ned Batchelder <ned@nedbatchelder.com> - 2013-05-01 18:01 -0400
  Re: How do I encode and decode this data to write to a file? Ned Batchelder <ned@nedbatchelder.com> - 2013-05-01 19:36 -0400

csiph-web