社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

用python修改json文件

brabbit640 • 5 年前 • 218 次点击  

我正在尝试编写一个python脚本,从json文件读取数据,对其进行一些计算,然后将输出写入一个新的json文件。但我似乎无法自动执行json读取过程。我知道这个错误。你能帮我解决这个问题吗? 非常感谢你

print([a[0]][b[1]][c[1]])
TypeError: list indices must be integers or slices, not str

杰森

{
      "male": {
            "jack": {
                  "id": "001",
                  "telephone": "+31 2225 345",
                  "address": "10 Street, Aukland",
                  "balance": "1500"
            },
            "john": {
                  "id": "002",
                  "telephone": "+31 6542 365",
                  "address": "Main street, Hanota",
                  "balance": "2500"
            }
      },

      "female": {
            "kay": {
                  "id": "00",
                  "telephone": "+31 6542 365",
                  "address": "Main street, Kiro",
                  "balance": "500"
            }
      }
}

Py

with open("q.json") as datafile:
    data = json.load(datafile)

    a = ['male', 'female']
    b = ['jack', 'john', 'kay']
    c = ['id', 'telephone', 'address', 'balance']

    print([a[1]][b[1]][c[1]])
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40264
 
218 次点击  
文章 [ 2 ]  |  最新文章 5 年前