[u'ABC']
would be a one-element list of unicode strings. Beautiful Soup always produces Unicode. So you need to convert the list to a single unicode string, and then convert that to ASCII.
I don’t know exaxtly how you got the one-element lists; the contents member would be a list of strings and tags, which is apparently not what you have. Assuming that you really always get a list with a single element, and that your test is really only ASCII you would use this:
soup[0].encode("ascii")
However, please double-check that your data is really ASCII. This is pretty rare. Much more likely it’s latin-1 or utf-8.
soup[0].encode("latin-1") soup[0].encode("utf-8")
Or you ask Beautiful Soup what the original encoding was and get it back in this encoding:
soup[0].encode(soup.originalEncoding)
Related Posts:
- Convert Unicode to ASCII without errors in Python
- How to resolve TypeError: can only concatenate str (not “int”) to str [duplicate]
- How to correct TypeError: Unicode-objects must be encoded before hashing?
- How to remove \xa0 from string in Python?
- How do I convert a list of ascii values to a string in python?
- How to get the ASCII value of a character
- What does the ‘b’ character do in front of a string literal?
- What does the ‘b’ character do in front of a string literal?
- (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
- Detect whether a Python string is a number or a letter
- (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
- “Unicode Error “unicodeescape” codec can’t decode bytes… Cannot open text files in Python 3
- Python – ‘ascii’ codec can’t decode byte
- UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xa0′ in position 20: ordinal not in range(128)
- Convert string to ASCII value python
- NameError: global name ‘unicode’ is not defined – in Python 3
- Convert a Unicode string to a string in Python (containing extra symbols)python string unicode type-conversion
- Using unicode character u201c
- What is a unicode string?
- What is an easy way to implement fprintf in python?
- Convert from ASCII string encoded in Hex to plain ASCII?
- TypeError: coercing to Unicode: need string or buffer, int found
- UnicodeDecodeError, invalid continuation byte
- What exactly do “u” and “r” string flags do, and what are raw string literals?
- How to write unicode strings into a file?
- ImportError: DLL load failed: The specified module could not be found
- Replacements for switch statement in Python?
- Correct way to write line to file?
- How to read a file line-by-line into a list?
- How do I sort a dictionary by value?
- “Defaulting to user installation because normal site-packages is not writeable” python message
- How to install pytorch in Anaconda with conda or pip?
- How can I remove a key from a Python dictionary?
- What does numpy.random.seed(0) do?
- How do I check if a variable exists?
- Is there a label/goto in Python?
- Why do I get “Pickle – EOFError: Ran out of input” reading an empty file?
- Getting TypeError: __init__() missing 1 required positional argument: ‘on_delete’ when trying to add parent table after child table with entries
- Python Pandas Counting the Occurrences of a Specific value
- What does the c underscore expression `c_` do exactly?
- Count unique values per groups with Pandas
- Get unique values from a list in python [duplicate]
- How do you decode Base64 data in Python?
- Convert pandas Series to DataFrame
- What’s the difference between %s and %d in Python string formatting?
- What is the difference between Jupyter Notebook and JupyterLab?
- Python 3 ImportError: No module named ‘ConfigParser’
- How does Python’s super() work with multiple inheritance?
- How can the Euclidean distance be calculated with NumPy?
- Tkinter understanding mainloop
- Embedding in pytorch
- Check string “None” or “not” in Python 2.7
- re.search().TypeError: cannot use a string pattern on a bytes-like object
- Cannot uninstall Tensorflow
- Add Legend to Seaborn point plot
- Get key by value in dictionary
- How to add multiple values to a dictionary key in python?
- Where can I find a list of the Flask SQLAlchemy Column types and options?
- pygame clock.tick() vs framerate in game main loop
- Use of “global” keyword in Python
- Convert a Pandas DataFrame to a dictionary
- Python: Write array values into file
- use np.random.multinomial() in python
- pg_config executable not found
- Import error when trying to import tensorflow with gpu
- Removing Punctuation From Python List Items
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- “OverflowError: Python int too large to convert to C long” on windows but not mac
- Python Array Rotation
- standard_init_linux.go:178: exec user process caused “exec format error”
- How to find char in string and get all the indexes?
- How can I create an array/list of dictionaries in python?
- How to unnest a nested list
- How to sort a list/tuple of lists/tuples by the element at a given index?
- What can lead to “IOError: [Errno 9] Bad file descriptor” during os.system()?
- Shebang doesn’t work with python3
- How do I make a dictionary with multiple keys to one value?
- Directing print output to a .txt file
- How to get a value from a cell of a dataframe?
- TypeError: can only concatenate str (not “float”) to str
- Converting a sentence to piglatin in Python
- How to use pygame.KEYDOWN to execute something every time through a loop while the key is held down?
- pandas groupby sort within groups
- Python Socket Receive Large Amount of Data
- What is a “slug” in Django?
- TypeError: Image data can not convert to float
- coercing to Unicode: need string or buffer, NoneType found when rendering in django admin
- Simple way to create matrix of random numbers
- pandas: multiple conditions while indexing data frame – unexpected behavior
- ‘instancemethod’ object has no attribute ‘__getitem__’ with class variables
- Python/Scikit-Learn – Can’t handle mix of multiclass and continuous
- python-How to set global variables in Flask?
- How do I install pip on macOS or OS X?
- NameError: name ‘get_ipython’ is not defined
- Equivalent to matlab’s imagesc in matplotlib? [duplicate]
- bash: syntax error near unexpected token `(‘ – Python
- Find all files in a directory with extension .txt in Python
- “public” or “private” attribute in Python ? What is the best way?
- How to search for a string in text files?
- TypeError: ‘zip’ object is not subscriptable