python-unicode

Removing unicode \u2026 like characters in a string in python2.7

ε祈祈猫儿з 提交于 2019-11-26 02:36:20
问题 I have a string in python2.7 like this, This is some \\u03c0 text that has to be cleaned\\u2026! it\\u0027s annoying! How do i convert it to this, This is some text that has to be cleaned! its annoying! 回答1: Python 2.x >>> s 'This is some \\u03c0 text that has to be cleaned\\u2026! it\\u0027s annoying!' >>> print(s.decode('unicode_escape').encode('ascii','ignore')) This is some text that has to be cleaned! it's annoying! Python 3.x >>> s = 'This is some \u03c0 text that has to be cleaned

How to print Unicode character in Python?

淺唱寂寞╮ 提交于 2019-11-26 02:28:26
问题 I want to make a dictionary where English words point to Russian and French translations. How do I print out unicode characters in Python? Also, how do you store unicode chars in a variable? 回答1: To include Unicode characters in your Python source code, you can use Unicode escape characters in the form \u0123 in your string, and prefix the string literal with 'u'. Here's an example running in the Python interactive console: >>> print u'\u0420\u043e\u0441\u0441\u0438\u044f' Россия Strings

Python - 'ascii' codec can't decode byte

南笙酒味 提交于 2019-11-26 02:16:57
问题 I\'m really confused. I tried to encode but the error said can\'t decode... . >>> \"你好\".encode(\"utf8\") Traceback (most recent call last): File \"<stdin>\", line 1, in <module> UnicodeDecodeError: \'ascii\' codec can\'t decode byte 0xe4 in position 0: ordinal not in range(128) I know how to avoid the error with \"u\" prefix on the string. I\'m just wondering why the error is \"can\'t decode\" when encode was called. What is Python doing under the hood? 回答1: "你好".encode('utf-8') encode

UnicodeDecodeError: &#39;utf8&#39; codec can&#39;t decode byte 0x9c

心已入冬 提交于 2019-11-26 01:31:08
问题 I have a socket server that is supposed to receive UTF-8 valid characters from clients. The problem is some clients (mainly hackers) are sending all the wrong kind of data over it. I can easily distinguish the genuine client, but I am logging to files all the data sent so I can analyze it later. Sometimes I get characters like this œ that cause the UnicodeDecodeError error. I need to be able to make the string UTF-8 with or without those characters. Update: For my particular case the socket

UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode character u&#39;\xa0&#39; in position 20: ordinal not in range(128)

拟墨画扇 提交于 2019-11-26 01:18:07
问题 I\'m having problems dealing with unicode characters from text fetched from different web pages (on different sites). I am using BeautifulSoup. The problem is that the error is not always reproducible; it sometimes works with some pages, and sometimes, it barfs by throwing a UnicodeEncodeError . I have tried just about everything I can think of, and yet I have not found anything that works consistently without throwing some kind of Unicode-related error. One of the sections of code that is

SyntaxError: Non-ASCII character &#39;\xa3&#39; in file when function returns &#39;£&#39;

帅比萌擦擦* 提交于 2019-11-26 01:04:54
问题 Say I have a function: def NewFunction(): return \'£\' I want to print some stuff with a pound sign in front of it and it prints an error when I try to run this program, this error message is displayed: SyntaxError: Non-ASCII character \'\\xa3\' in file \'blah\' but no encoding declared; see http://www.python.org/peps/pep-0263.html for details Can anyone inform me how I can include a pound sign in my return function? I\'m basically using it in a class and it\'s within the \'__str__\' part