python

Encoding issue of str() for list or tuple in python2  #angularjs #reactjs

  • It’s working perfect while I don’t get local letters in error message.
  • python string list encoding tuples
  • Stack Overflow works best with JavaScript enabled
  • In my script I get error message from DB and want to show it in a web page.
  • I’m not receiving error, but output on the web page like this: “(1, ‘\xd0\xad\xd1\x82\xd0\xbe \xd0\xbc\xd0\xbe\xd0\xb9 \xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x82’)” – Mikhail Yuldashev 3 hours ago

In my script I get error message from DB and want to show it in a web page. It’s working perfect while I don’t get local letters in error message.
Message is tuple.

@ng_real_ninja: Encoding issue of str() for list or tuple in python2 #angularjs #reactjs

In my script I get error message from DB and want to show it in a web page. It’s working perfect while I don’t get local letters in error message. Message is tuple.

>>> # coding: utf-8 … # string with u >>> data1 = (1, u’Это мой текст’) >>> print data1[1] Это мой текст >>> print str(data1) (1, u’\u042d\u0442\u043e \u043c\u043e\u0439 \u0442\u0435\u043a\u0441\u0442′) # string without u >>> data2 = (1, ‘Это мой текст’) >>> print data2[1] Это мой текст >>> print str(data2) (1, ‘\xd0\xad\xd1\x82\xd0\xbe \xd0\xbc\xd0\xbe\xd0\xb9 \xd1\x82\xd0\xb5\xd0\xba\xd1\x81\xd1\x82’) # I want like this >>> str(data2).??? # WHAT I HAVE TO DO??? (1, ‘Это мой текст’) # Current solution for me, but I want to know about better and more beautiful solution >>> print ‘, ‘.join(map(lambda x: x.decode(‘utf-8’) if isinstance(x, str) else str(x), data2)) 1, Это мой текст

Can you help me find (???) method and explain why it’s happening. Thanks!!!

python