file.write() of non-ASCII characters differs in Interpreted Python than in script run

RAH rene.heymans at gmail.com
Wed Aug 26 11:02:28 EDT 2015


Dear All,

First, thanks to each and everyone.
There is indeed a solution by I haven't yet found the root of the problem (I'll come back to that at the end of my post).

1) After many trials and errors, I found that the problem was with the write() function in `h.write(str(d) + '\n')` and not with the argument itself which is a perfect string.

2) Reading the documentation it refers to the open() function and its preferred encoding.

3) I checked with the interpreter and got:

    rse at Alibaba:~/test$ python
    Python 3.4.0 (default, Jun 19 2015, 14:18:46)
    [GCC 4.8.2] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from locale import *
    >>> getpreferredencoding()
    'UTF-8'
    >>>

4) I knew everything was set up with UTF-8 (refer my first answer to Chris K.) so I couldn't believe it ! Another dead end ?

5) I had to make sure it was the same within the application, so I added a couple of statements to get and record the preferred encoding. And lo and behold I got his:

    rse at Alibaba:~/test$ cat type
    ANSI_X3.4-1968    <class 'bytes'>   <class 'str'>   <class 'dict'>
    rse at Alibaba:~/test$

So, here the getpreferredencoding() function returns ANSI_X3.4-1968 instead of UTF-8 !?

6) The solution is then obvious: open the file by specifying the encoding; a suggestion made already by Chris A.

7) Now, that source of the problem is known, I must investigate why my run-time environment differs from the interpreter environment. I know it is the same machine, same Python 3.4.0. As the mod_wsgi module in Apache2 initiates Python for the run-time, I will look there around.

Dear All,
Thank you each and everyone for your contribution. I would suggest to close this subject. If I get a solution around mod_wsgi + python I will post it.
Kind regards to all,
René







More information about the Python-list mailing list