Python3 trim 关联数组中的所有数据
Python3 trim all data from associative array
我正在使用 Python3 并且我尝试了所有方法 question 但没有一个解决方案有效。如果我这样做 list(map(str.strip, my_list))
我丢失了所有密钥,因为 list()
return 只是可以通过索引访问的值。
所以我决定 trim 手动使用 .strip()
我的所有数据,但它不适用于 NoneType
数据。我不想做30个条件...
if str:
str.strip(' ')
那么,对于 trim 我的关联数组中的所有 str 值,您有解决方案吗?我的数组可以包含 None、Int 和 String.
我假设当你说 "associative array" 你实际上是指 Python 字典。
>>> d = { 'a': 'foo\n', 'b': 3, 'c': None }
>>> cleaned = { k: v.strip() if isinstance(v, str) else v for k,v in d.items() }
>>> cleaned
{'b': 3, 'a': 'foo', 'c': None}
我正在使用 Python3 并且我尝试了所有方法 question 但没有一个解决方案有效。如果我这样做 list(map(str.strip, my_list))
我丢失了所有密钥,因为 list()
return 只是可以通过索引访问的值。
所以我决定 trim 手动使用 .strip()
我的所有数据,但它不适用于 NoneType
数据。我不想做30个条件...
if str:
str.strip(' ')
那么,对于 trim 我的关联数组中的所有 str 值,您有解决方案吗?我的数组可以包含 None、Int 和 String.
我假设当你说 "associative array" 你实际上是指 Python 字典。
>>> d = { 'a': 'foo\n', 'b': 3, 'c': None }
>>> cleaned = { k: v.strip() if isinstance(v, str) else v for k,v in d.items() }
>>> cleaned
{'b': 3, 'a': 'foo', 'c': None}