problem with (unicode)strings

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

problem with (unicode)strings

Torsten
Hello group.

I'm trying to get a list of the network adapters of the computer and list them in a frame with StaticText(). Some of my adapters have german chars (äöü) in their names. When i print() them the names are shown correct, but with StaticText() they are wrong. Example:
Name of the adapter is "Täst".
print()  shows  'Täst'
StaticText()  shows 'T„st'

I tried unicode(), encode() and decode() with different codecs, but wasn't able to correctly show the name with StaticText().

Where do i miss something?

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

ListAdapters.pyw (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: problem with (unicode)strings

Niki Spahiev-2
On 16.07.2014 19:28, Torsten wrote:

> Hello group.
>
> I'm trying to get a list of the network adapters of the computer and
> list them in a frame with StaticText(). Some of my adapters have german
> chars (äöü) in their names. When i print() them the names are shown
> correct, but with StaticText() they are wrong. Example:
>
>     Name of the adapter is "Täst".
>
>     print()  shows  'Täst'
>
>     StaticText()  shows 'T„st'


What is you wxPython version?
Try with unicode build of wxPython.

Niki

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: problem with (unicode)strings

Torsten
I tried it with Python 2.7.8 / wxPython 2.9.5.0 msw (classic) and Python 3.4.1 / wxPython 3.0.2.dev76938 msw (phoenix). Neither i were able to get the correct result.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: problem with (unicode)strings

werner-3
Hi Torsten,

On 7/17/2014 10:35, Torsten wrote:
> I tried it with Python 2.7.8 / wxPython 2.9.5.0 msw (classic) and
> Python 3.4.1 / wxPython 3.0.2.dev76938 msw (phoenix). Neither i were
> able to get the correct result.
Hhm, can understand it on Py2.7 but on Py3.4 it should work without
doing much.

I tried to run your script on Win8.1, Py3.4 and Phoenix, but to get it
to run I had to change it, see attached.

It would run forever if I kept 'shell=True'.

Main change is that in Py3 the proc.stdout.readline() returns bytes so
had to decode.

I also added '/nh' to the command, to suppress the headers.

BTW, how do you get "Umlaute" into these connection names?

Werner

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

getmac.py (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: problem with (unicode)strings

Torsten


Am Donnerstag, 17. Juli 2014 11:58:48 UTC+2 schrieb werner:
Hi Torsten,

On 7/17/2014 10:35, Torsten wrote:
> I tried it with Python 2.7.8 / wxPython 2.9.5.0 msw (classic) and
> Python 3.4.1 / wxPython 3.0.2.dev76938 msw (phoenix). Neither i were
> able to get the correct result.
Hhm, can understand it on Py2.7 but on Py3.4 it should work without
doing much.

I tried to run your script on Win8.1, Py3.4 and Phoenix, but to get it
to run I had to change it, see attached.

Yes, i also did some changes to make it work with 3.4 (it was my first time using Python 3). Sorry, should have mention that and attach the file.

It would run forever if I kept 'shell=True'.

Main change is that in Py3 the proc.stdout.readline() returns bytes so
had to decode.

I also added '/nh' to the command, to suppress the headers.

When i run your script with Python 3 i get a UnicodeDecodeError:

Traceback (most recent call last):
  File "D:\Downloads\getmac.py", line 42, in <module>
    win = MainFrame()
  File "D:\Downloads\getmac.py", line 10, in __init__
    adapters = self.GetAdapters()
  File "D:\Downloads\getmac.py", line 31, in GetAdapters
    infos = line.decode('utf-8').split('","')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x84 in position 17: invalid start byte
 
 
BTW, how do you get "Umlaute" into these connection names?

In Windows in the Network settings i just renamed one adapter.


--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: problem with (unicode)strings

werner-3
Hi Torsten,

On 7/17/2014 12:15, Torsten wrote:


Am Donnerstag, 17. Juli 2014 11:58:48 UTC+2 schrieb werner:
Hi Torsten,

On 7/17/2014 10:35, Torsten wrote:
> I tried it with Python 2.7.8 / wxPython 2.9.5.0 msw (classic) and
> Python 3.4.1 / wxPython 3.0.2.dev76938 msw (phoenix). Neither i were
> able to get the correct result.
Hhm, can understand it on Py2.7 but on Py3.4 it should work without
doing much.

I tried to run your script on Win8.1, Py3.4 and Phoenix, but to get it
to run I had to change it, see attached.

Yes, i also did some changes to make it work with 3.4 (it was my first time using Python 3). Sorry, should have mention that and attach the file.

It would run forever if I kept 'shell=True'.

Main change is that in Py3 the proc.stdout.readline() returns bytes so
had to decode.

I also added '/nh' to the command, to suppress the headers.

When i run your script with Python 3 i get a UnicodeDecodeError:

Traceback (most recent call last):
  File "D:\Downloads\getmac.py", line 42, in <module>
    win = MainFrame()
  File "D:\Downloads\getmac.py", line 10, in __init__
    adapters = self.GetAdapters()
  File "D:\Downloads\getmac.py", line 31, in GetAdapters
    infos = line.decode('utf-8').split('","')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x84 in position 17: invalid start byte
O.K. after changing my network name I see this too.

So, no idea what stdout uses as encoding, tried a few others such as "ISO-8859-1", "cp1252" but no luck.

Doing a bit of googling I came across:
http://bugs.python.org/issue6135

But can't find if this has been resolved/introduced in Py3.4.

Werner

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: problem with (unicode)strings

wxPython-users mailing list-2
On 17/07/2014 12:11, Werner wrote:

> Hi Torsten,
>
> On 7/17/2014 12:15, Torsten wrote:
>>
>>
>> Am Donnerstag, 17. Juli 2014 11:58:48 UTC+2 schrieb werner:
>>
>>     Hi Torsten,
>>
>>     On 7/17/2014 10:35, Torsten wrote:
>>     > I tried it with Python 2.7.8 / wxPython 2.9.5.0 msw (classic) and
>>     > Python 3.4.1 / wxPython 3.0.2.dev76938 msw (phoenix). Neither i
>>     were
>>     > able to get the correct result.
>>     Hhm, can understand it on Py2.7 but on Py3.4 it should work without
>>     doing much.
>>
>>     I tried to run your script on Win8.1, Py3.4 and Phoenix, but to
>>     get it
>>     to run I had to change it, see attached.
>>
>> Yes, i also did some changes to make it work with 3.4 (it was my first
>> time using Python 3). Sorry, should have mention that and attach the file.
>>
>>     It would run forever if I kept 'shell=True'.
>>
>>     Main change is that in Py3 the proc.stdout.readline() returns
>>     bytes so
>>     had to decode.
>>
>>     I also added '/nh' to the command, to suppress the headers.
>>
>> When i run your script with Python 3 i get a UnicodeDecodeError:
>>
>>     Traceback (most recent call last):
>>       File "D:\Downloads\getmac.py", line 42, in <module>
>>         win = MainFrame()
>>       File "D:\Downloads\getmac.py", line 10, in __init__
>>         adapters = self.GetAdapters()
>>       File "D:\Downloads\getmac.py", line 31, in GetAdapters
>>         infos = line.decode('utf-8').split('","')
>>     UnicodeDecodeError: 'utf-8' codec can't decode byte 0x84 in
>>     position 17: invalid start byte
>>
> O.K. after changing my network name I see this too.
>
> So, no idea what stdout uses as encoding, tried a few others such as
> "ISO-8859-1", "cp1252" but no luck.
>
> Doing a bit of googling I came across:
> http://bugs.python.org/issue6135
>
> But can't find if this has been resolved/introduced in Py3.4.
>
> Werner
>

Did you try the work around attached to issue 6135
http://bugs.python.org/file28760/subProcessTest.py ?

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: problem with (unicode)strings

Florian Höch
In reply to this post by werner-3
Hi,

Am 17.07.2014 13:11, schrieb Werner:

>> When i run your script with Python 3 i get a UnicodeDecodeError:
>>
>>     Traceback (most recent call last):
>>       File "D:\Downloads\getmac.py", line 42, in <module>
>>         win = MainFrame()
>>       File "D:\Downloads\getmac.py", line 10, in __init__
>>         adapters = self.GetAdapters()
>>       File "D:\Downloads\getmac.py", line 31, in GetAdapters
>>         infos = line.decode('utf-8').split('","')
>>     UnicodeDecodeError: 'utf-8' codec can't decode byte 0x84 in
>>     position 17: invalid start byte
>>
> O.K. after changing my network name I see this too.
>
> So, no idea what stdout uses as encoding, tried a few others such as
> "ISO-8859-1", "cp1252" but no luck.
>
> Doing a bit of googling I came across:
> http://bugs.python.org/issue6135
>
> But can't find if this has been resolved/introduced in Py3.4.

Under Windows, you need to decode subprocess input and output with the
console input and output encodings. The only way to reliably get that
(which I know of) is to use Python for Windows extensions (pywin32). E.g.:


    import subprocess
    from encodings.aliases import aliases
    from win32console import GetConsoleOutputCP

    # Get console output encoding
    enc = aliases.get(str(GetConsoleOutputCP()))

    p = subprocess.Popen(r'getmac /V /FO CSV',
                         stdout=sp.PIPE, stderr=sp.PIPE)
    stdout, stderr = [v.decode(enc) for v in p.communicate()]



(shell=True is only necessary for shell commands by the way, e.g. things
like 'dir' etc.)

This works under Python 2.x and 3.x.

If you're using Python 2.x, one caveat applies if you localize your
application (i.e. if you use locale.setlocale and derivatives):

In that case, the encoding of stdio changes, and you need to use the
encoding specific to the locale being used, i.e. locale.getlocale()[1].

--
Florian Höch

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: problem with (unicode)strings

werner-3
Hi Torsten,

On 7/17/2014 17:29, Florian Höch wrote:
...

> Under Windows, you need to decode subprocess input and output with the
> console input and output encodings. The only way to reliably get that
> (which I know of) is to use Python for Windows extensions (pywin32). E.g.:
>
>
>      import subprocess
>      from encodings.aliases import aliases
>      from win32console import GetConsoleOutputCP
>
>      # Get console output encoding
>      enc = aliases.get(str(GetConsoleOutputCP()))
>
>      p = subprocess.Popen(r'getmac /V /FO CSV',
>                           stdout=sp.PIPE, stderr=sp.PIPE)
>      stdout, stderr = [v.decode(enc) for v in p.communicate()]
>
>
>
> (shell=True is only necessary for shell commands by the way, e.g. things
> like 'dir' etc.)
>
> This works under Python 2.x and 3.x.
>
> If you're using Python 2.x, one caveat applies if you localize your
> application (i.e. if you use locale.setlocale and derivatives):
>
> In that case, the encoding of stdio changes, and you need to use the
> encoding specific to the locale being used, i.e. locale.getlocale()[1].
>
Adapted your script with Florian's suggestion and that works for me on
Py3.4.

On my system the encoding is 'cp850'

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

getmac.py (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: problem with (unicode)strings

Florian Höch
One thing I forgot to say, if you run with the windowed python
executable (pythonw.exe) you have to use GetOEMCP instead of
GetConsoleOutputCP because the latter will always return 0 in that case.

Also, you can use ctypes instead of pywin32, e.g.

import ctypes
ctypes.windll.kernel32.GetOEMCP()

Am 17.07.2014 21:02, schrieb Werner:
> Adapted your script with Florian's suggestion and that works for me on
> Py3.4.
>
> On my system the encoding is 'cp850'

--
Florian Höch

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.