码迷,mamicode.com
首页 > 其他好文 > 详细

format函数报错:ValueError: dictionary update sequence element #0 has length 1; 2 is required,str转dict

时间:2019-05-10 11:15:59      阅读:515      评论:0      收藏:0      [点我收藏+]

标签:cti   update   %s   word   ict   href   sequence   orm   date   

一个字典通过format()函数转换后就会由dict变为str,

例如:

a = "156464654654"
b = "652683126541"
c = "{‘username‘:‘%s‘,‘password‘:‘%s‘,‘Submit‘: ‘dre‘}"%(a,b)
# c = eval("{‘username‘:‘%s‘,‘password‘:‘%s‘,‘Submit‘: ‘dre‘}"%(a,b))
print(type(c))    #(输出<class ‘str‘>)

  

所以在用的时候需要再将str转为dict,这里用到的转换为eval(),

例如:

a = "156464654654"
b = "652683126541"
# c = "{‘username‘:‘%s‘,‘password‘:‘%s‘,‘Submit‘: ‘dre‘}"%(a,b)
c = eval("{‘username‘:‘%s‘,‘password‘:‘%s‘,‘Submit‘: ‘dre‘}"%(a,b))
print(type(c))    #(输出<class "dict">)

  附上原链接:https://blog.csdn.net/weixin_40894428/article/details/80683137

format函数报错:ValueError: dictionary update sequence element #0 has length 1; 2 is required,str转dict

标签:cti   update   %s   word   ict   href   sequence   orm   date   

原文地址:https://www.cnblogs.com/qiaoer1993/p/10843191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!