Posts

Showing posts with the label unicode

How to fix "F821 undefined name 'unicode'" error when running tox

If you get this following error when running "tox -e pep8": ... # F821 undefined name 'unicode' if isinstance(config, str) or isinstance(config, unicode): ... Modify the tox.ini as following to fix it: [flake8] builtins = unicode

How to remove accents from unicode characters using python

Sometimes unicode characters with accents cause you trouble (actually most of the time). You can even get rid of those characters or replace them with the base ones (without accents). Here is how to do the second method in Python: import unidecode your_text = u"Nguyễn Trọng Đăng Trình" your_non_accent_text = unidecode(your_text).encode('ascii') print your_non_accent_text Nguyen Trong Dang Trinh Reference:   https://pypi.python.org/pypi/Unidecode

Python - ROT13 encode

"ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher, developed in ancient Rome." ~Wikipedia. Here are 2 solutions to implement ROT13 in Python: 0. 1st solution: import string ROT13 = string.maketrans(\          "ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz",\          "NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm") def rot13_encode (user_text):     return string.translate(user_text, ROT13) This method works smoothly in the console but when using with the web it will raise error because of some special characters in the input: File "/usr/lib/python27/string.py" , line 498 , in translate return s . translate ( table + s [: 0 ]) UnicodeDecodeError : 'ascii' codec can 't decode byte 0x80...