emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Daniele Pizzolli <dan@toel.it>
To: emacs-orgmode@gnu.org
Subject: org-mode, python and unicode
Date: Wed, 18 May 2016 10:10:21 +0200	[thread overview]
Message-ID: <8737pfzzzzu6qa@me.localhost.invalid> (raw)

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
“this string is not ascii!”
#+END_EXAMPLE

#+NAME: error-in-passing-var
#+BEGIN_SRC python :var data=unicode_str
return data
#+END_SRC

#+RESULTS: error-in-passing-var

Will result in the following message in the buffer =*Org-Babel Error Output*=:

#+BEGIN_EXAMPLE
  File "<stdin>", line 3
SyntaxError: Non-ASCII character '\xe2' in file <stdin> on line 3, but no encoding 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 =:preamble= with wthe value =# -*- coding:utf-8 -*-=

#+NAME: ok-in-passing-var
#+BEGIN_SRC python :preamble "# -*- coding: utf-8 -*-" :var data=unicode_str
return data
#+END_SRC

#+RESULTS: ok-in-passing-var
: “this string is not ascii!”

** passing utf-8 tables to python

#+NAME: unicode_table
| key | value                       |
|-----+-----------------------------|
|   1 | “this string is not ascii!” |

#+NAME: error-in-passing-table
#+BEGIN_SRC python :var data=unicode_table
return data
#+END_SRC

Will result in the following message in the buffer =*Org-Babel Error Output*=:

#+BEGIN_EXAMPLE
  File "<stdin>", line 3
SyntaxError: Non-ASCII character '\xe2' in file <stdin> on line 3, but no encoding 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=unicode_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=unicode_table
return data[0][1]
#+END_SRC

#+RESULTS: returnig-table-cell
: “this string is not ascii!”

** 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

             reply	other threads:[~2016-05-18  8:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-18  8:10 Daniele Pizzolli [this message]
2016-05-19 22:36 ` org-mode, python and unicode William Henney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8737pfzzzzu6qa@me.localhost.invalid \
    --to=dan@toel.it \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).