Sunday, April 27, 2014

Re: What is different of handling character code between pure Python and Django?

-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - https://gpgtools.org

iQEcBAEBCgAGBQJTXRlPAAoJELMiOgVOfjkQ9fEH/0E/baP3gyq72GUC4PjetMdk
Ni/MP8rokgkxX+h1DQK8A6LeNcmTte3IKwj/PE3p1mvZJdMHEWoinC1bMmaljrgP
IHlMTESNDUcEBR0u6s13Vg0QHOvFyNmLg17pVtKixBQhfXWic1qBqGqHlp8ZgyxV
Fjpn2B31hvdFufyIKUp6BGzAi4zkXyn71PLeHyhb6momeBu7J791t3wXZK/I1gpz
jlOPoquz72BcfIyarLDcwkOETcpmm+VDUjGW6AyzYbBVXMulo08dAho9+m9FpRBi
h7qd/Oq3ap+FnzT91wyPH/WKUpi62QcCtXuuzQfpaomFzdcyIigerqiHjkkIsCM=
=JoFx
-----END PGP SIGNATURE-----
You should check the encoding of stdout when running from django, I suspect that it is plain ascii rather than utf-8 which is what you are probably getting when running standalone. Check sys.getdefaultencoding().

Note that this has nothing to do with django, just the way stdin/stdout are set up depending on how your script is running.

Also see:

http://stackoverflow.com/questions/1473577/writing-unicode-strings-via-sys-stdout-in-python
http://stackoverflow.com/questions/15740236/stdout-encoding-in-python
http://stackoverflow.com/questions/492483/setting-the-correct-encoding-when-piping-stdout-in-python

François

On Apr 27, 2014, at 2:13 AM, Sugita Shinsuke <shinriyo@gmail.com> wrote:

> Hi there
>
> I'd like to run Java code via Django.
>
> The Java code, javaprogram use like below.
>
> —
> java javaprogram [text] [file_name]
> —
>
> text is parameter. multi-byte character is also okey.
> file_name is generate file name.
>
> So, I run the stand-alone Python program like below could run fine.
> —
> java_file = 'javaprogram'
> file_name = 'filename'
> text = 'あいうえお' #Japanese character
> java_file_path = '/path/to/'
> class_path = '-cp ' + java_file_path
>
> cmd = "java {0} {1} {2} {3}".format(class_path, java_file, text, file_name)
>
> import subprocess
> proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
> —
>
> But, I run same program in Django.
> It couldn't work. However, if text is English, it works fine.
>
> What is different of handling character code between pure Python and Django?
> And, could you tell me how to resolve it?
>
> Thank you.
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cd081056-2a39-40f5-94c8-7b470bf827ea%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment