我尝试过尝试cat块,以验证字典中的字典,并且字典中的列表是否具有unicode。
def convert(input):
if isinstance(input, dict):
for key, value in input.iteritems():
temp = {convert(key):convert(value)}
return temp
elif isinstance(input, list):
for element in input:
temp1 = [convert(element)]
return temp1
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
#return input
print input
如果具有unicode,字典中的元素或使用utf8
编码的元素,我想从此函数得到结果。但是我遇到了这样的错误:
$python main.py
File "main.py", line 21
return temp1
^
IndentationError: unindent does not match any outer indentation level
答案 0 :(得分:0)
只要您声明if else
for
循环class
定义或function
定义之类的块,期望缩进级别,缩进就像大括号一样与空格或制表符相同的缩进级别将其视为一个块。如果要嵌套块,请执行以下操作:
def myfunction(arg): #one block
for x in range(0,arg): #indents at starting
if x==1:#another block
print('one')#more indents for nesting
所以在您的代码中
def convert(input):
if isinstance(input, dict):
for key, value in input.iteritems():
temp = {convert(key):convert(value)}
return temp
elif isinstance(input, list):
for element in input:
temp1 = [convert(element)]
return temp1 #here it expects to be in same indent level temp1 = [convert(element)] is at
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
#return input
print input
在@Andronicus给出的答案中已解决