我正在使用Python 3.7。如何从字符串中删除所有非UTF-8字符?我尝试在下面使用“ lambda x:x.decode('utf-8','ignore')。encode(“ utf-8”)“
coop_types = map(
lambda x: x.decode('utf-8','ignore').encode("utf-8"),
filter(None, set(d['type'] for d in input_file))
)
但是这会导致错误...
Traceback (most recent call last):
File "scripts/parse_coop_csv.py", line 30, in <module>
for coop_type in coop_types:
File "scripts/parse_coop_csv.py", line 25, in <lambda>
lambda x: x.decode('utf-8','ignore').encode("utf-8"),
AttributeError: 'str' object has no attribute 'decode'
如果您具有从字符串中删除所有非UTF8字符的通用方法,那就是我要寻找的全部。
您以字符串开头。您不能decode
一个str
。并且假定UTF-8应该编码任何有效的Unicode文本(str
存储的内容),每个str
应该可以[[encode-作为UTF-8,所以您的目标似乎很荒谬。