我想在dict python中验证unicode

时间:2019-02-11 05:16:04

标签: python validation serialization

我尝试过尝试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

1 个答案:

答案 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给出的答案中已解决