Problem with encoding

Denis S. Otkidach ods at
Wed Aug 21 12:57:22 CEST 2002

On 21 Aug 2002, Bach wrote:

B> There are a problem when I write program in Python2.2 (I try
B> modify
B> for that):
B>     1)create a socket:   self._sock=socket.socket(...)
B>     2)Initialaze :data_in = u''
B>     3)Get data: data_in = data_in +
B>        unicode(self._sock.recv(BLOCK_SIZE))
B>  I want to get the data from socket (data send by another
B> "correct"
B>  program with encode UTF-8 ) and convert to Unicode.When I
B> try to

Probably you should use "unicode(self._sock.recv(BLOCK_SIZE),
'utf-8')" or even "unicode(self._sock.recv(BLOCK_SIZE),
'utf-8', 'replace')" to ignore errors for corrupted data.

B>  get Russian letters make an error: UnicodeError: ASCII
B> encoding
B>  error: ordinal not in range(128).

The default encoding assumed by unicode() is 7 bit ASCII.

B> My program :Create a jabber-client which could send and
B> receive data
B> in UTF-8  encode.
B> Idea:1)Send data :Convert data to unicode --> Encode in UTF-8
B> --> Send
B>                   u_data=unicode(data)
B> ;_sock.send(u_data.encode)
B>      2)Receive: Get data -->convert to unicode
B>                    g_data=unicode(_sock.recv(SIZE))

Denis S. Otkidach      [ru] [ru]

More information about the Python-list mailing list