进入编写阿里云在线的OCR程序,也顺便提前了json字符串和python字典的转换及使用,由于涉及到字典的变量问题,顺便写了一个迭代dict字典类的函数,供大家参考学习。
# dict字典迭代函数
def dictIterator(value, key:str ='', i:int = -1):# value为字典的value,key为字典的key,i为字典所在层级数
i +=1
if type(value) ==dict:# 判断输入类型是为字典类型
for (k, v)in value.items():# 如果是字典类型则遍历字典原始
dictIterator(v, k, i)# 并回调迭代函数自身
elif type(value) ==list:# 如果类型为list,则为字典对象数组,则做数组遍历出字典对象
for datain value:
for (data_k, data_v)in data.items():# 遍历数组中单个字典对象,并回调迭代函数自身
dictIterator(data_v, data_k, i)
else:# 如果既不是字典对象,也不是数组对象,则直接输出
tabStr =''
for bin range(i):
tabStr +=' ' # 在打印过程中体现不同层级缩进
print('Level[', i, "]:", tabStr, key, ":", value)
jsonStr = {"sid":"0cc4688a7e9c551cfedf59450a72c703952e400512d674c2bbd261b73ff022080e23ea03","prism_version":"1.0.9","prism_wnum":5,"prism_wordsInfo":[{"word":"Open pilot的主要特性包括哪些?","pos":[{"x":59,"y":43},{"x":497,"y":40},{"x":497,"y":67},{"x":60,"y":70}],"direction":0},{"word":"Open pilot官方版本支持车道保持与ACC巡航。","pos":[{"x":97,"y":113},{"x":527,"y":112},{"x":528,"y":134},{"x":97,"y":135}],"direction":0},{"word":"分支版本arne 182\u0027s Fork也有对部分车型支持转向灯变道, 依赖于原车盲区检测功能提供侧向车道检测","pos":[{"x":95,"y":163},{"x":1018,"y":163},{"x":1018,"y":185},{"x":95,"y":185}],"direction":0},{"word":"信号,丰田车型适配较好。","pos":[{"x":58,"y":198},{"x":291,"y":198},{"x":291,"y":218},{"x":58,"y":218}],"direction":0},{"word":"·0.6.4之后的版本在测试过程中发现,如果没有车道线情况下,会跟随前车轨迹行驶","pos":[{"x":78,"y":249},{"x":841,"y":249},{"x":841,"y":269},{"x":78,"y":269}],"direction":0}],"height":286,"width":1040,"orgHeight":286,"orgWidth":1040,"content":"Open pilot的主要特性包括哪些? Open pilot官方版本支持车道保持与ACC巡航。 分支版本arne 182\u0027s Fork也有对部分车型支持转向灯变道, 依赖于原车盲区检测功能提供侧向车道检测 信号,丰田车型适配较好。 ·0.6.4之后的版本在测试过程中发现,如果没有车道线情况下,会跟随前车轨迹行驶 "}
dictIterator(jsonStr)