如何使用关键字在 json 文件中搜索特定行?
How do I search for a certain line in a json file with a key word?
我想知道如何搜索具有特定名称的行。这是我的 .txt 文件 json:
{"people": [{"name": "Scott", "website": "stackabuse.com", "from": "Nebraska"}, {"name": "Larry", "website": "google.com", "from": "Michigan"}, {"name": "Tim", "website": "apple.com", "from": "Alabama"}]}
这是我的代码
import json
file = open('data.txt', "r")
read = file.read()
y = json.loads(read)
first = y["people"][0]
second = y["people"][1]
third = y["people"][2]
print(y["people"][0]["name"])
打印出 Scott,但是有没有办法在 json 文件中搜索名为 Scott 的行?我试过 print(y["people"]["name": "Scott"]) 但这没有用。我希望输出为 {"name": "Scott", "website": "stackabuse.com", "from": "Nebraska"}
我建议使用 jmespath (https://pypi.org/project/jmespath/)
您可以使用 list comprehension 来过滤列表。例如
>>> people = y['people']
>>> people_named_scott = [p for p in people if p['name'] == 'Scott']
>>> people_named_scott
[{'name': 'Scott', 'website': 'stackabuse.com', 'from': 'Nebraska'}]
如果我没理解错的话,您正在尝试打印名为 Scott 的 json 的列表。
print([jobj if jobj['name']=='Scott' for jobj in y['people']])
您可以执行类似这样的操作,它会遍历所有人,然后 returns 具有匹配名称的第一个人。
def getPersonByName(allInfo, name):
for info in allInfo["people"]:
if info["name"] == name:
return info
allInfo = {"people": [{"name": "Scott", "website": "stackabuse.com", "from": "Nebraska"}, {"name": "Larry", "website": "google.com", "from": "Michigan"}, {"name": "Tim", "website": "apple.com", "from": "Alabama"}]}
print(getPersonByName(allInfo, "Scott"))
您可以将姓名为Scott
的人的信息存储在列表中。
Python代码:
import json
file = open('data.txt', "r")
read = file.read()
y = json.loads(read)
people = y["people"]
lines = [person for person in people if person["name"] == "Scott"]
if len(lines) == 0:
print("Scott is not found")
else:
for line in lines:
print(line)
data.txt
:
{"people": [{"name": "Scott", "website": "stackabuse.com", "from": "Nebraska"}, {"name": "Larry", "website": "google.com", "from": "Michigan"}, {"name": "Tim", "website": "apple.com", "from": "Alabama"}]}
输出:
{'name': 'Scott', 'website': 'stackabuse.com', 'from': 'Nebraska'}
您可以使用内置的 filter
function combined with a lambda
表达式来求解。
data={"people": [{"name": "Scott", "website": "stackabuse.com", "from": "Nebraska"}, {"name": "Larry", "website": "google.com", "from": "Michigan"}, {"name": "Tim", "website": "apple.com", "from": "Alabama"}]}
print(filter(lambda x:x['name'] == 'Scott',data['people']))
# gives [{'website': 'stackabuse.com', 'from': 'Nebraska', 'name': 'Scott'}]
我想知道如何搜索具有特定名称的行。这是我的 .txt 文件 json:
{"people": [{"name": "Scott", "website": "stackabuse.com", "from": "Nebraska"}, {"name": "Larry", "website": "google.com", "from": "Michigan"}, {"name": "Tim", "website": "apple.com", "from": "Alabama"}]}
这是我的代码
import json
file = open('data.txt', "r")
read = file.read()
y = json.loads(read)
first = y["people"][0]
second = y["people"][1]
third = y["people"][2]
print(y["people"][0]["name"])
打印出 Scott,但是有没有办法在 json 文件中搜索名为 Scott 的行?我试过 print(y["people"]["name": "Scott"]) 但这没有用。我希望输出为 {"name": "Scott", "website": "stackabuse.com", "from": "Nebraska"}
我建议使用 jmespath (https://pypi.org/project/jmespath/)
您可以使用 list comprehension 来过滤列表。例如
>>> people = y['people']
>>> people_named_scott = [p for p in people if p['name'] == 'Scott']
>>> people_named_scott
[{'name': 'Scott', 'website': 'stackabuse.com', 'from': 'Nebraska'}]
如果我没理解错的话,您正在尝试打印名为 Scott 的 json 的列表。
print([jobj if jobj['name']=='Scott' for jobj in y['people']])
您可以执行类似这样的操作,它会遍历所有人,然后 returns 具有匹配名称的第一个人。
def getPersonByName(allInfo, name):
for info in allInfo["people"]:
if info["name"] == name:
return info
allInfo = {"people": [{"name": "Scott", "website": "stackabuse.com", "from": "Nebraska"}, {"name": "Larry", "website": "google.com", "from": "Michigan"}, {"name": "Tim", "website": "apple.com", "from": "Alabama"}]}
print(getPersonByName(allInfo, "Scott"))
您可以将姓名为Scott
的人的信息存储在列表中。
Python代码:
import json
file = open('data.txt', "r")
read = file.read()
y = json.loads(read)
people = y["people"]
lines = [person for person in people if person["name"] == "Scott"]
if len(lines) == 0:
print("Scott is not found")
else:
for line in lines:
print(line)
data.txt
:
{"people": [{"name": "Scott", "website": "stackabuse.com", "from": "Nebraska"}, {"name": "Larry", "website": "google.com", "from": "Michigan"}, {"name": "Tim", "website": "apple.com", "from": "Alabama"}]}
输出:
{'name': 'Scott', 'website': 'stackabuse.com', 'from': 'Nebraska'}
您可以使用内置的 filter
function combined with a lambda
表达式来求解。
data={"people": [{"name": "Scott", "website": "stackabuse.com", "from": "Nebraska"}, {"name": "Larry", "website": "google.com", "from": "Michigan"}, {"name": "Tim", "website": "apple.com", "from": "Alabama"}]}
print(filter(lambda x:x['name'] == 'Scott',data['people']))
# gives [{'website': 'stackabuse.com', 'from': 'Nebraska', 'name': 'Scott'}]