From mboxrd@z Thu Jan 1 00:00:00 1970 From: Daniele Pizzolli Subject: org-mode, python and unicode Date: Wed, 18 May 2016 10:10:21 +0200 Message-ID: <8737pfzzzzu6qa@me.localhost.invalid> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:57389) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b2wYq-0007bc-D6 for emacs-orgmode@gnu.org; Wed, 18 May 2016 04:10:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b2wYk-0007FB-Di for emacs-orgmode@gnu.org; Wed, 18 May 2016 04:10:32 -0400 Received: from slow1-d.mail.gandi.net ([217.70.178.86]:38016) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b2wYk-0007F7-6a for emacs-orgmode@gnu.org; Wed, 18 May 2016 04:10:26 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by slow1-d.mail.gandi.net (Postfix) with ESMTP id 5513347E3BF for ; Wed, 18 May 2016 10:10:25 +0200 (CEST) Received: from localhost.invalid (outusers6.fbk.eu [217.77.82.229]) (Authenticated sender: me@toel.it) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id EC45D41C0A4 for ; Wed, 18 May 2016 10:10:24 +0200 (CEST) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org Hello, I found some problems and workarounds in the use of python (version 2) and org-mode. This applies to: #+BEGIN_EXAMPLE Org-mode version 8.3.4 (8.3.4-60-g19cf68-elpa @ /home/vagrant/.emacs.d/elpa= /org-20160516/) #+END_EXAMPLE Some workaround are not complete, and suggestions are welcome! Best, Daniele * python ** load python support for babel #+BEGIN_SRC emacs-lisp (org-babel-do-load-languages 'org-babel-load-languages '((python . t))) #+END_SRC #+RESULTS: : ((python . t)) ** passing utf-8 strings to python #+NAME: unicode_str #+BEGIN_EXAMPLE =E2=80=9Cthis string is not ascii!=E2=80=9D #+END_EXAMPLE #+NAME: error-in-passing-var #+BEGIN_SRC python :var data=3Dunicode_str return data #+END_SRC #+RESULTS: error-in-passing-var Will result in the following message in the buffer =3D*Org-Babel Error Outp= ut*=3D: #+BEGIN_EXAMPLE File "", line 3 SyntaxError: Non-ASCII character '\xe2' in file on line 3, but no e= ncoding declared; see http://www.python.org/peps/pep-0263.html for details #+END_EXAMPLE ** passing utf-8 strings to python with workaround A workaround is to use =3D:preamble=3D with wthe value =3D# -*- coding:utf-= 8 -*-=3D #+NAME: ok-in-passing-var #+BEGIN_SRC python :preamble "# -*- coding: utf-8 -*-" :var data=3Dunicode_= str return data #+END_SRC #+RESULTS: ok-in-passing-var : =E2=80=9Cthis string is not ascii!=E2=80=9D ** passing utf-8 tables to python #+NAME: unicode_table | key | value | |-----+-----------------------------| | 1 | =E2=80=9Cthis string is not ascii!=E2=80=9D | #+NAME: error-in-passing-table #+BEGIN_SRC python :var data=3Dunicode_table return data #+END_SRC Will result in the following message in the buffer =3D*Org-Babel Error Outp= ut*=3D: #+BEGIN_EXAMPLE File "", line 3 SyntaxError: Non-ASCII character '\xe2' in file on line 3, but no e= ncoding declared; see http://www.python.org/peps/pep-0263.html for details #+END_EXAMPLE ** returning utf-8 tables from python #+NAME: error-in-returnig-table #+BEGIN_SRC python :preamble "# -*- coding: utf-8 -*-" :var data=3Dunicode_= table :colnames yes return data #+END_SRC Will output the ascii text with escape sequence for unicode characters. I still do not know the best workaround #+RESULTS: error-in-returnig-table | key | value | |-----+---------------------------------------------------| | 1 | \xe2\x80\x9cthis string is not ascii!\xe2\x80\x9d | Please note that the single cell is correctly returned: #+NAME: returnig-table-cell #+BEGIN_SRC python :preamble "# -*- coding: utf-8 -*-" :var data=3Dunicode_= table return data[0][1] #+END_SRC #+RESULTS: returnig-table-cell : =E2=80=9Cthis string is not ascii!=E2=80=9D ** Undocumented parts I did not found out: - how to get the table headers in python - in general, the magic that org-mode does to pass and retrieve data structures to various languages is lacking documentation