本文介绍了从dict中删除空格:Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑我有口语。Ex。
dict1 = {1434:{2012-10-29:{275174 :{declaration_details:
{UTCC:38483,CNRE:8334,CASH:55096.0},
sales_details:{UTCC :38483.0,CNRE:8334.0,现金:
55098.0}},275126:{declaration_details:{CNIS:63371},
sales_details:{CNIS:63371.0}},275176:
{declaration_details:{UTCC:129909,CASH:93200.0`,
CNRE:28999,PBGV:1700},sales_details:{UTCC:
131619.0,PBGV:1700.0,CASH:92880.0 ,CNRE:28999.0}},
275169:{declaration_details:{AMCC:118616,CNRE:19462,
CASH 120678.0},sales_details:{UTCC:118616.0,CNRE:
19462.0,CASH:120677.0}},266741:{declaration_details $ b {UTCC:42678,CNRE:4119,CASH:24944.0`},
sales_details:{UTCC:42678.0,CNRE :4119.0,CASH:
24944.0}}}}}
我想要删除该dict1中的所有空格。
哪个更好的方法?
解决方案
def removew(d):
for k,v in d.iteritems():
if isinstance(v,dict):
removew(v)
else:
d [k] = v.strip()
removew(dict1)
print dict1
输出:
<$ c $'{'1434':{'2012-10-29':{'275174':{'declaration_details':{'UTCC':'38483','CNRE':'8334','CASH':'55096.0 '','sales_details':{'UTCC':'38483.0','CNRE':'8334.0','CASH':'55098.0'}},'275126':{'declaration_details':{'CNIS':'63371 '','sales_details':{'CNIS':'63371.0'}},'275176':{'declaration_details':{'UTCC':'129909','CNRE':'28999','CASH':'93200.0 ','PBGV':'1700'},'sales_details':{'UTCC':'131619.0','CNRE' ''''''''''''''''''''''''''''''''' ''''''''',''''''''''''''''' ':'42678','CNRE':'4119','CASH':'24944.0'},'sales_details':{'UTCC':'42678.0','CNRE':'4119.0','CASH':'24944.0 '}}}}}
编辑:
如Blckknght所述,第一个解决方案,将会中断,如果您 strip()
包含空格(旧键,值对保留在dict中)的键。如果你需要使用dict的理解,返回一个新的dict(从python 2.7开始可用)。
def removew(d) :
return {k.strip():removew(v)
if isinstance(v,dict)
else v.strip()
for k,v in d.iteritems ()}
removew(dict1)
Consider i have dict.Ex.
dict1 = {"1434": {"2012-10-29": {"275174": {"declaration_details":
{"UTCC": `"38483 "`, "CNRE": "8334", "CASH": "55096.0"},
"sales_details": {"UTCC": "38483.0", "CNRE": "8334.0", "CASH":
"55098.0"}}, "275126": {"declaration_details": {"CNIS": "63371"},
"sales_details": {"CNIS": "63371.0"}}, "275176":
{"declaration_details": {"UTCC": "129909", "CASH": `"93200.0 "`,
"CNRE": "28999", "PBGV": "1700"}, "sales_details": {"UTCC":
"131619.0", "PBGV": "1700.0", "CASH": "92880.0", "CNRE": "28999.0"}},
"275169": {"declaration_details": {"AMCC": "118616", "CNRE": "19462",
"CASH": "120678.0"}, "sales_details": {"UTCC": "118616.0", "CNRE":
"19462.0", "CASH": "120677.0"}}, "266741": {"declaration_details":
{"UTCC": "42678", "CNRE": "4119", "CASH": `"24944.0 "`},
"sales_details": {"UTCC": "42678.0", "CNRE": "4119.0", "CASH":
"24944.0"}}}}}
i wanted to removed all white spaces in that dict1.
Which is the better way to do that?
解决方案
def removew(d):
for k, v in d.iteritems():
if isinstance(v, dict):
removew(v)
else:
d[k]=v.strip()
removew(dict1)
print dict1
output:
{'1434': {'2012-10-29': {'275174': {'declaration_details': {'UTCC': '38483', 'CNRE': '8334', 'CASH': '55096.0'}, 'sales_details': {'UTCC': '38483.0', 'CNRE': '8334.0', 'CASH': '55098.0'}}, '275126': {'declaration_details': {'CNIS': '63371'}, 'sales_details': {'CNIS': '63371.0'}}, '275176': {'declaration_details': {'UTCC': '129909', 'CNRE': '28999', 'CASH': '93200.0', 'PBGV': '1700'}, 'sales_details': {'UTCC': '131619.0', 'CNRE': '28999.0', 'CASH': '92880.0', 'PBGV': '1700.0'}}, '275169': {'declaration_details': {'CNRE': '19462', 'AMCC': '118616', 'CASH': '120678.0'}, 'sales_details': {'UTCC': '118616.0', 'CNRE': '19462.0', 'CASH': '120677.0'}}, '266741': {'declaration_details': {'UTCC': '42678', 'CNRE': '4119', 'CASH': '24944.0'}, 'sales_details': {'UTCC': '42678.0', 'CNRE': '4119.0', 'CASH': '24944.0'}}}}}
EDIT:As noted by Blckknght,the first solution, will break, if you strip()
keys that contain whitespace (old key,value pairs remain in the dict). If you need to strip both use dict comprehension, returning a new dict (available since python 2.7).
def removew(d):
return {k.strip():removew(v)
if isinstance(v, dict)
else v.strip()
for k, v in d.iteritems()}
removew(dict1)
这篇关于从dict中删除空格:Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-30 08:51