Removing u in list
That 'u' is part of the external representation of the string, meaning it's a Unicode string as opposed to a byte string. It's not in the string, it's part of the type.
As an example, you can create a new Unicode string literal by using the same synax. For instance:
>>> sandwich = u"smörgås"
>>> sandwich
u'sm\xf6rg\xe5s'
This creates a new Unicode string whose value is the Swedish word for sandwich. You can see that the non-English characters are represented by their Unicode code points, ö is \xf6
and å is \xe5
. The 'u' prefix appears just like in your example to signify that this string holds Unicode text.
To get rid of those, you need to encode the Unicode string into some byte-oriented representation, such as UTF-8. You can do that with e.g.:
>>> sandwich.encode("utf-8")
'sm\xc3\xb6rg\xc3\xa5s'
Here, we get a new string without the prefix 'u', since this is a byte string. It contains the bytes representing the characters of the Unicode string, with the Swedish characters resulting in multiple bytes due to the wonders of the UTF-8 encoding.
Remove 'u' from a python list
In your current code, you are iterating on a string, which represents a list, hence you get the individual characters.
>>> from ast import literal_eval
>>> l = [u'[190215]']
>>> l = [item for value in l for item in value]
>>> l
[u'[', u'1', u'9', u'0', u'2', u'1', u'5', u']']
Seems to me, you want to convert the inner string representation of list, to a flattened list, so here you go:
>>> l = [u'[190215]']
>>> l = [item for value in l for item in literal_eval(value)]
>>> l
[190215]
The above will work only when all the inner lists are strings:
>>> l = [u'[190215]', u'[190216, 190217]']
>>> l = [item for value in l for item in literal_eval(value)]
>>> l
[190215, 190216, 190217]
>>> l = [u'[190215]', u'[190216, 190217]', [12, 12]]
>>> l = [item for value in l for item in literal_eval(value)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "/usr/lib/python2.7/ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string
What is the simplest way to remove unicode 'u' from a list
This is the simplest solution
d=[{u'Length': u'2.96m', u'Width': u'1.44m', u'height': u'0.38m'},
{u'Length': u'3.8m', u'Width': u'0.65m', u'height': u'9.3m'},
{u'Length': u'0.62m', u'Width': u'2.9m', u'height': u'3.5m'}]
def to_utf8(d):
final = []
for item in d:
if type(item) is dict:
result = {}
for key, value in item.items():
result[str(key)] = str(value)
final.append(result)
return final
print to_utf8(d)
How to strip unicode in a list
First, I strongly suggest you switch to Python 3, which treats Unicode strings as first-class citizens (all strings are Unicode strings, but they are called str
).
But if you have to make it work in Python 2, you can strip unicode
strings with unicode.strip
(if your strings are true Unicode strings):
>>> lst = [u'KATL\n', u'KCID\n']
>>> map(unicode.strip, lst)
[u'KATL', u'KCID']
If your unicode
strings are limited to ASCII subset, you can convert them to str
with:
>>> lst = [u'KATL', u'KCID']
>>> map(str, lst)
['KATL', 'KCID']
Note that this conversion will fail for non-ASCII strings. To encode Unicode codepoints as a str
(string of bytes), you have to choose your encoding algorithm (usually UTF-8) and use .encode()
method on your strings:
>>> lst = [u'KATL', u'KCID']
>>> map(lambda x: x.encode('utf-8'), lst)
['KATL', 'KCID']
How to remove ('u')unicode from list of dictionaries?
You can also encode you unicode characters in utf-8 like:
In [2]: d = [{'email': u'123@gmail.com', 'name': u'xxx'}, {'email': u'abc@gmail.com', 'name': u'xxx1'}, {'email': u'xyz@gmail.com', 'name': u'xxx2'}]
In [3]: new_d = [{k: v.encode("utf-8") for k, v in elem.items()} for elem in d]
In [4]: new_d
Out[4]:
[{'email': '123@gmail.com', 'name': 'xxx'},
{'email': 'abc@gmail.com', 'name': 'xxx1'},
{'email': 'xyz@gmail.com', 'name': 'xxx2'}]
How to remove Specific values in python list
First of all, your list contains tuple which contains string. And tuple doesn't support remove
Just convert tuples as list and then use remove
>>> res = list(result[0])
['ABC', '(Choose field)', 'ABCD', 'aa', 'A', 'A_100']
>>> res.remove('(Choose field)')
['ABC', 'ABCD', 'aa', 'A', 'A_100']
Related Topics
Why Is Using Thread Locals in Django Bad
Find Nearest Indices for One Array Against All Values in Another Array - Python/Numpy
How to Check If a String Only Contains Letters
Writing Utf-8 String to MySQL with Python
How to Make a Barplot and a Lineplot in the Same Seaborn Plot with Different Y Axes Nicely
Python Datetime to String Without Microsecond Component
Installing Multiple Versions of a Package with Pip
Return and Yield in the Same Function
Convert Structured Array to Regular Numpy Array
Time Complexity of Accessing a Python Dict
How Does Keras Calculate the Accuracy
Python SQLite Parameter Substitution with Wildcards in Like