我试图从Python 3.5中的字典列表中删除空格。从字典列表中删除空格
我有这样的:
lst_of_dicts = [{'code': 'AB DE', 'score': 30},
{'code': 'DE FG', 'score': 40}]
而想要这样的:
lst_of_dicts = [{'code': 'ABDE', 'score': 30},
{'code': 'DEFG', 'score': 40}]
其他answers已经使用字典理解建议.strip()
,比如这个:
clean_d = { k:v.strip() for k, v in d.iteritems()}
但我避风港由于我是一个蟒蛇新手,我无法使它适用于我的数据结构。
你可能有 '分数' 的问题,因为它的类型'int'和'int'没有.strip() –
对于k ,v in item.items(): item [k] = v.replace('','')试试这个 –
[''str.strip()'](https://docs.python.org/3/ library/stdtypes.html#str.strip)返回一个字符串的副本,其中*前导字符和尾部字符被移除。您正在寻找['str.replace()'](https://docs.python.org/3/library/stdtypes.html#str.replace)。 –