遇到问题

ExtendValue =  {"area": "1","info": "{\"year\": 2014, \"a\": 12, \"b\": 3, \"c\":5}","trip_country": "CN"}

在按照字典访问的时候,报错。TypeError: string indices must be integers, not str,意思是索引必须是int型不能是字符型。

错误原因

(出这种错误有多种可能,我只记录我遇到的)

经查找发现,是json格式导致的错误,info的value是json数据,python无法直接识别。

解决办法

原来字典存储的对象是json,因此需要把json反解码后才可以读取。

要json.loads(),才能把json格式转为python识别的格式。

加上一行代码:

ExtendValue["info"]=json.loads(ExtendValue["info"])

拓展

Python json 模块dumps、dump、loads、load的使用

json.dumps将python对象格式化成json字符(将dict转化成str)
json.loads将json字符串解码成python对象(将str转化成dict)
json_str = json.dumps(data)  # 编码
data = json.loads(json_str)  # 解码
json.dump主要用来将python对象写入json文件
f = open('demo.json','w',encoding='utf-8')
json.dump(decode_json,f,ensure_ascii=False)
f.close()
json.load加载json格式文件,返回python对象
f = open('demo.json','r',encoding='utf-8')
data = json.load(f)
print(data,type(data))
f.close()

解决TypeError: string indices must be integers, not str相关推荐

  1. Python 错误:TypeError String Indices Must be Integers 【已解决】

    Python 错误:TypeError String Indices Must be Integers [已解决] 如果你尝试使用字符串值,而不是整数值来访问字典或可迭代对象的值,那么你将收到以下错误 ...

  2. TypeError: string indices must be integers

    1.错误描述 >>> print(st[-1,-6]); Traceback (most recent call last):File "<pyshell#13> ...

  3. 成功解决TypeError: slice indices must be integers or None or have an __index__ method

    成功解决TypeError: slice indices must be integers or None or have an __index__ method 目录 解决问题 解决思路 解决方法 ...

  4. 成功解决TypeError: tuple indices must be integers or slices, not str

    成功解决TypeError: tuple indices must be integers or slices, not str 目录 解决问题 解决思路 解决方法 解决问题 TypeError: t ...

  5. Python错误:TypeError: string indices must be integers

    在项目遇到一个错误 : TypeError: string indices must be integers 由于是返回的接口数据,所以一时也分辨不出是哪里的错,然后我就还原了这样的场景: unico ...

  6. python之解析 yaml配置文件时TypeError: string indices must be integers

    #定义一个SuperLogin登录函数,传递3个形参数 def SuperLogin(lANIP,username, password):driver = webdriver.Chrome()driv ...

  7. python报错 TypeError: string indices must be integers

    所以在读取字典的时候,最好先判断类型,然后再查看它是否已经有这样的属性: type(mydict) == type({})             #检查不是字典 如果是字典,再看看有没有这样的属性: ...

  8. python3 错误string indices must be integers 的解决方法

    这个错误意思是字符串的下标一定要是整数 出这种错误有多种可能,最形象直接的就是: a = '[abc]' print(a['0']) 有点pyhton基础的都知道下标怎么能是字符串'0',必须是整数0 ...

  9. string indices must be integers错误原因

    string indices must be integers:字符串索引必须是整数 一.使用字符串为索引 a = [1, 2, 3, 4] print(a['0']) '0'为字符串,0才是整形.这 ...

最新文章

  1. BCH协议升级倒计时——超过68%的BCH全节点已支持升级
  2. linux内核引入模块机制,Linux内核设备驱动之Linux内核模块加载机制笔记整理
  3. Javascript刷新全集
  4. 如何卸载安装有问题的devexpress组件?
  5. 用python画图代码意思_Python科学画图代码分享
  6. 语C、耽美、盲盒、Lolita……这些95后文化,你看懂了吗?
  7. Sharding-JDBC(操作公共表)_Sharding-Sphere,Sharding-JDBC分布式_分库分表工作笔记013
  8. C语言的变参函数设计
  9. java对账_Java微信对账接口返回值解析
  10. 在Vue文件中引用模块的相对路径“@“符号表示什么意思?
  11. 判断滚动条到底部的JS代码
  12. Getting a handle on
  13. 为什么做AI的都选Python?
  14. 常见排序算法之快速排序
  15. 2018年秋招笔试面试----小学渣求职历险记(中南篇)
  16. jvm 调优 2020.09.07
  17. android x86 鼠标指针,x86中断完全版
  18. 【WCN6856】WiFi 5G 接口启动失败问题解决
  19. 吴恩达机器学习[9]-神经网络学习
  20. 【罗技】M590 鼠标驱动

热门文章

  1. 在 PHP 中使用命令行工具
  2. Linux 性能监测
  3. Magento 获取有效属性 Display available options for attributes of Configurable
  4. Flash AS3获取PHP数据(ActionScript3 + php + email)送ActionScript2
  5. mysql基本表管理sql语句
  6. 【AI视野·今日CV 计算机视觉论文速览 第156期】Mon, 9 Sep 2019
  7. JVM—调优参数学习
  8. 控件绑定有序对象集合winform
  9. 演练 小说排行榜 0912
  10. java数组 0912