python json解析工具选择_配置解析器Python如何读取json文件?

问题是您正试图将字典键作为属性(即:config["given_name"]vsconfig.given_name)访问。你也多次改变你的问题,但你想做的(我认为)很简单。对于您给出的一个简单示例,其中有一个json文件,其中只有一个json对象,这可能更接近您要做的事情:

*注意:json语法错误,应该是{ "info": { ... } }#!/usr/bin/env python3

'''profile.json:

{

"name": "steve",

"lastname": "jobs",

"age": "70",

"city": "heaven"

}

'''

import json

import io

# Open the JSON File and create a StringIO buffer to hold data

with open('profile.json', 'r') as datafile, io.StringIO() as data:

# Load data into json file

config = json.load(datafile)

# Build json strong

data.write(f'''{{

\r\t"info": {{

\r\t\t"given_name": "{config['name']}",

\r\t\t"given_Lastname": "{config['lastname']}",

\r\t\t"given_age": "{config['age']}",

\r\t\t"given_city": "{config['city']}"

\r\t}}\n}}''')

print(data.getvalue())

# open a new file to save the data (overwrite if it exists)

with open('newfile.json', 'w') as outfile:

# load the json string and dump to outfile

deserialized = json.loads(data.getvalue())

json.dump(deserialized, outfile)

# newfile.json:

#{

# "info": {

# "given_name": "steve",

# "given_Lastname": "jobs",

# "given_age": "70",

# "given_city": "heaven"

# }

#}

这只是你给我的数据的一个小例子,所以我制作了另一个使用json列表而不是json dict的例子:[{

"name": "steve",

"lastname": "jobs",

"age": "70",

"city": "heaven"

},

{

"name": "steve1",

"lastname": "jobs1",

"age": "71",

"city": "heaven1"

},

{

"name": "steve2",

"lastname": "jobs2",

"age": "72",

"city": "heaven2"

},

{

"name": "steve3",

"lastname": "jobs3",

"age": "73",

"city": "heaven3"

},

{

"name": "steve4",

"lastname": "jobs4",

"age": "74",

"city": "heaven4"

}]

还有一个类似的剧本:#!/usr/bin/env python3

'''profile.json:

'''

import json

import io

# Open the JSON File and create a StringIO buffer to hold data

# Note: StringIO provides a file-like interface over a string

with open('profile.json', 'r') as datafile, io.StringIO() as data:

# Load data into json file

config = json.load(datafile)

# Build json strong

data.write('{\n\t"info": [\n')

#data.write('\t{')

for jsonobj in config:

data.write(f'''\t {{

\r\t\t"given_name": "{jsonobj['name']}",

\r\t\t"given_Lastname": "{jsonobj['lastname']}",

\r\t\t"given_age": "{jsonobj['age']}",

\r\t\t"given_city": "{jsonobj['city']}"

\r\t }}''')

# Note: There is a bug here.

# This will not be able to handle duplicate objects in

# the json list. For a trivial example like this, it works.

if jsonobj == config[-1]:

data.write('\n\t]\n}')

else:

data.write(',\n')

# open a new file to save the data (overwrite if it exists)

with open('newfile.json', 'w') as outfile:

# We need to serialize the json data if we want to write to file

deserialized = json.loads(data.getvalue())

outfile.write(json.dumps(serialized))

# or we can just print it

print(serialized)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值