python,如何在json文件中找到特定的键值然后保存整行json文件?
python, How to find specific key value in json file then save whole line of json file?
我是新手,我需要在json文件中导出特定的键值,我搜索了很多网站都没有找到我需要的代码,希望这对我有帮助
我的json文件
{"person":{"name":"Silva","sex":"female","age":21}}
{"person":{"name":"LANA","sex":"male","age":28}}
{"person":{"name":"Oliveira","sex":"female","age":35}}
{"person":{"name":"KENN","sex":"male","age":26}}
需要导出整行 json 文件,其中 'sex' 是 'male' 在 JSON 文件中
{"person":{"name":"LANA","sex":"male","age":28}}
{"person":{"name":"KENN","sex":"male","age":26}}
需要完整代码
您需要展示您尝试过的内容,以便我们了解您期望的格式。然而,这是解决它的最简单方法之一 -
我假设您有一个 JSON 文件,其中存储了所有男性和女性的详细信息 (每条记录以逗号分隔,并且仅使用 double-quotes ""
不是单个 ''
!您提供的 JSON 文件无效)
从这里您需要导入 json,然后使用 json.load 转换为 python 对象(在本例中它将是一个列表),然后迭代它以检查每个字典中的键 'sex'
并进行相应的过滤。完整代码:
import json
filename = "<filename>.json"
with open(filename, "rt") as file:
data = json.load(file)
new_data = []
for record in data:
if record['person']['sex'] == 'male':
new_data.append(record)
newfile = "<new_filename>.json"
with open(newfile, "wt") as file:
json.dump(new_data, file)
要使此代码正常工作,初始 JSON 文件(上面的 "<filename>.json"
)应采用以下格式:
[
{"person":{"name":"Silva","sex":"female","age":21}},
{"person":{"name":"LANA","sex":"male","age":28}},
{"person":{"name":"Oliveira","sex":"female","age":35}},
{"person":{"name":"KENN","sex":"male","age":26}}
]
PS:请避免在 stackoverflow 上询问作业问题的完整解决方案。我们还有很多其他工作要做。
使用:
import json
s = json.dumps([{'person':{'name':'Silva','sex':'female','age':21}},
{'person':{'name':'LANA','sex':'male','age':28}},
{'person':{'name':'Oliveira','sex':'female','age':35}},
{'person':{'name':'KENN','sex':'male','age':26}}])
如果您有一个 json 文件,那么:
s = 'file_path'
s = pd.read_json(s)
s = s['person'].apply(pd.Series)
s[s['sex']=='male']
在这里,我将 json 转换为 df,然后将每一行转换为单独的列,然后过滤 df。
请记住,输入文件不是 JSON 文件 本身 。每行都是有效的 JSON,因此必须单独处理。
例如:
import json
with open ('foonew.txt', 'w', encoding='utf-8') as out:
with open('foo.txt', encoding='utf-8') as j:
for line in j:
if (d := json.loads(line))['person']['sex'] == 'male':
print(json.dumps(d), file=out)
输出文件如下所示:
{"person": {"name": "LANA", "sex": "male", "age": 28}}
{"person": {"name": "KENN", "sex": "male", "age": 26}}
或者,要为每个性别获取一个单独的文件,则:
import json
sexes = []
with open('foo.txt', encoding='utf-8') as j:
for line in j:
d = json.loads(line)
sex = d['person']['sex']
if sex in sexes:
mode = 'a'
else:
sexes.append(sex)
mode = 'w'
with open(f'{sex}.txt', mode, encoding='utf-8') as out:
print(json.dumps(d), file=out)
我是新手,我需要在json文件中导出特定的键值,我搜索了很多网站都没有找到我需要的代码,希望这对我有帮助
我的json文件
{"person":{"name":"Silva","sex":"female","age":21}}
{"person":{"name":"LANA","sex":"male","age":28}}
{"person":{"name":"Oliveira","sex":"female","age":35}}
{"person":{"name":"KENN","sex":"male","age":26}}
需要导出整行 json 文件,其中 'sex' 是 'male' 在 JSON 文件中
{"person":{"name":"LANA","sex":"male","age":28}}
{"person":{"name":"KENN","sex":"male","age":26}}
需要完整代码
您需要展示您尝试过的内容,以便我们了解您期望的格式。然而,这是解决它的最简单方法之一 -
我假设您有一个 JSON 文件,其中存储了所有男性和女性的详细信息 (每条记录以逗号分隔,并且仅使用 double-quotes ""
不是单个 ''
!您提供的 JSON 文件无效)
从这里您需要导入 json,然后使用 json.load 转换为 python 对象(在本例中它将是一个列表),然后迭代它以检查每个字典中的键 'sex'
并进行相应的过滤。完整代码:
import json
filename = "<filename>.json"
with open(filename, "rt") as file:
data = json.load(file)
new_data = []
for record in data:
if record['person']['sex'] == 'male':
new_data.append(record)
newfile = "<new_filename>.json"
with open(newfile, "wt") as file:
json.dump(new_data, file)
要使此代码正常工作,初始 JSON 文件(上面的 "<filename>.json"
)应采用以下格式:
[
{"person":{"name":"Silva","sex":"female","age":21}},
{"person":{"name":"LANA","sex":"male","age":28}},
{"person":{"name":"Oliveira","sex":"female","age":35}},
{"person":{"name":"KENN","sex":"male","age":26}}
]
PS:请避免在 stackoverflow 上询问作业问题的完整解决方案。我们还有很多其他工作要做。
使用:
import json
s = json.dumps([{'person':{'name':'Silva','sex':'female','age':21}},
{'person':{'name':'LANA','sex':'male','age':28}},
{'person':{'name':'Oliveira','sex':'female','age':35}},
{'person':{'name':'KENN','sex':'male','age':26}}])
如果您有一个 json 文件,那么:
s = 'file_path'
s = pd.read_json(s)
s = s['person'].apply(pd.Series)
s[s['sex']=='male']
在这里,我将 json 转换为 df,然后将每一行转换为单独的列,然后过滤 df。
请记住,输入文件不是 JSON 文件 本身 。每行都是有效的 JSON,因此必须单独处理。
例如:
import json
with open ('foonew.txt', 'w', encoding='utf-8') as out:
with open('foo.txt', encoding='utf-8') as j:
for line in j:
if (d := json.loads(line))['person']['sex'] == 'male':
print(json.dumps(d), file=out)
输出文件如下所示:
{"person": {"name": "LANA", "sex": "male", "age": 28}}
{"person": {"name": "KENN", "sex": "male", "age": 26}}
或者,要为每个性别获取一个单独的文件,则:
import json
sexes = []
with open('foo.txt', encoding='utf-8') as j:
for line in j:
d = json.loads(line)
sex = d['person']['sex']
if sex in sexes:
mode = 'a'
else:
sexes.append(sex)
mode = 'w'
with open(f'{sex}.txt', mode, encoding='utf-8') as out:
print(json.dumps(d), file=out)