"你好".encode('utf-8')
encode
converts a unicode object to a string
object. But here you have invoked it on a string
object (because you don’t have the u). So python has to convert the string
to a unicode
object first. So it does the equivalent of
"你好".decode().encode('utf-8')
But the decode fails because the string isn’t valid ascii. That’s why you get a complaint about not being able to decode.
Related Posts:
- UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xa0′ in position 20: ordinal not in range(128)
- How to remove \xa0 from string in Python?
- TypeError: super() takes at least 1 argument (0 given) error is specific to any python version?
- What exactly do “u” and “r” string flags do, and what are raw string literals?
- TypeError: ‘int’ object is not subscriptable
- What is the difference between rw+ and r+
- raw_input function in Python
- How to resolve TypeError: can only concatenate str (not “int”) to str [duplicate]
- Cannot find module cv2 when using OpenCV
- Python ‘If not’ syntax [duplicate]
- RuntimeWarning: invalid value encountered in divide
- How to correct TypeError: Unicode-objects must be encoded before hashing?
- WinError 2 The system cannot find the file specified (Python)
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- TypeError: cannot perform reduce with flexible type
- Could not find a version that satisfies the requirement tensorflow
- sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype(‘float64’)
- Local variable referenced before assignment?
- ln (Natural Log) in Python
- ImportError: numpy.core.multiarray failed to import
- Python Traceback (most recent call last)
- write() versus writelines() and concatenated strings
- Unable to plot Double Bar, Bar plot using pyplot for ndarray
- How to pip or easy_install tkinter on Windows
- ImportError: numpy.core.multiarray failed to import
- No module named MySQLdb
- What is the meaning of [:] in python [duplicate]
- No module named MySQLdb
- Why are Python’s ‘private’ methods not actually private?
- Remove list from list in Python
- python socket.error: [Errno 98] Address already in use
- How to import files in python using sys.path.append?
- How do I install the yaml package for Python?
- Change figure size and figure format in matplotlib
- How can I concatenate str and int objects?
- What does the ‘b’ character do in front of a string literal?
- how does \r (carriage return) work in Python
- What is Python buffer type for?
- Add list to set?
- Create 3D array using Python
- How do you use subprocess.check_output() in Python?
- Checking whether pip is installed?
- Converting binary to decimal integer output
- Decoding UTF-8 strings in Python
- How can I read pdf in python?
- How to have an array of arrays in Python
- ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
- ValueError: unsupported pickle protocol: 3, python2 pickle can not load the file dumped by python 3 pickle?
- AttributeError(“‘str’ object has no attribute ‘read'”)
- “Unicode Error “unicodeescape” codec can’t decode bytes… Cannot open text files in Python 3
- Why does using from __future__ import print_function breaks Python2-style print?
- How to fix: “UnicodeDecodeError: ‘ascii’ codec can’t decode byte”
- How to return dictionary keys as a list in Python?
- Python Error – int object has no attribute
- Difference between scikit-learn and sklearn
- Loading a file with more than one line of JSON into Pandas
- How to detect key presses?
- What is the difference between json.load() and json.loads() functions
- How to XOR two strings in Python
- TypeError: ‘_io.TextIOWrapper’ object is not subscriptable
- Copy a list of list by value and not reference
- Python 101: Can’t open file: No such file or directory
- Split a python list into other “sublists” i.e smaller lists
- TypeError: argument of type ‘NoneType’ is not iterable
- scrapy run spider from script
- python error: TypeError: an integer is required
- Convert a Unicode string to a string in Python (containing extra symbols)python string unicode type-conversion
- If list index exists, do X
- How to detect key presses?
- Installation of pygame with Anaconda
- How to print Unicode character in Python?
- Using unicode character u201c
- urllib and “SSL: CERTIFICATE_VERIFY_FAILED” Error
- ‘virtualenv’ is not recognized as an internal or external command, operable program or batch file
- ImportError: cannot import name NUMPY_MKL
- Python: Pandas pd.read_excel giving ImportError: Install xlrd >= 0.9.0 for Excel support
- How can I copy a Python string?
- Convert string to variable name in python
- What is the meaning of “int(a[::-1])” in Python?
- dump() missing 1 required positional argument: ‘fp’ in python json
- How to completely uninstall python 2.7.13 on Ubuntu 16.04
- Convert Unicode to ASCII without errors in Python
- Spell Checker for Python
- TypeError: coercing to Unicode: need string or buffer, int found
- How to open html file?
- “Python version 2.7 required, which was not found in the registry” error when attempting to install netCDF4 on Windows 8
- Installing scipy for python 2.7
- Does python have header files like C/C++?
- How can I install a .egg Python package on Windows (attempt using easy_install not working)
- Determine if 2 lists have the same elements, regardless of order? [duplicate]
- In python, how can I print lines that do NOT contain a certain string, rather than print lines which DO contain a certain string:
- input() error – NameError: name ‘…’ is not defined
- Python 2.7 mixing iteration and read methods would lose data
- Homebrew brew doctor warning about /Library/Frameworks/Python.framework, even with brew’s Python installed
- What does an ‘r’ represent before a string in python?
- Cannot remove entries from nonexistent file
- How to read a CSV file from a URL with Python?
- Python: OSError: [Errno 2] No such file or directory: ”
- How to import a csv-file into a data array?