我的案例研究的一个问题让我困惑,(JSON相关)

bqujaahr  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(134)

因此,我有一个实习案例研究,它有问题检索JSON数据并显示数据如下源:https://workwithus.pinpointhq.com/jobs.json与过滤器:workplace_type_text =完全远程我知道workplace_type_text是关键字,我应该使用它进行过滤。但是JSON文件不像通常的文件那样结构化,很容易从中检索数据。我该怎么办?

import json

with open('case_study.json', encoding='utf-8') as file:
    data = json.load(file)

print(data)  # Check the structure and content of the loaded JSON data

filtered_data = [item for item in data if item['workplace_type_text'] == 'Fully remote']

for item in filtered_data:
    print(item['name'], item['price'])

我试过这段代码,但它只打印了整个文件,并显示了一个TypeError:字符串索引必须是整数,而不是filtered_data = [item for item in data if item['workplace_type_text'] == 'Fully remote']的'str'
所以我把它改成了

import json

with open('case_study.json', encoding='utf-8') as file:
    data = json.load(file)

filtered_data = [item for item in data if 'workplace_type_text' in item and item['workplace_type_text'] == 'Fully remote']

print(filtered_data)  # Check the contents of the filtered_data list

for item in filtered_data:
    print(item['name'], item['price'])

现在它只打印[],其他什么都不打印
尝试检查钥匙是否正确

import json

with open('case_study.json', encoding='utf-8') as file:
    data = json.load(file)

print(data.keys())

也不打印任何东西。我迷路了,我跳过了一步吗?谢谢你。

b4qexyjb

b4qexyjb1#

要从“case_study.json”中检索信息,您可以轻松地利用pandas从JSON文件中的'data'对象创建DataFrame。
然后,如果需要,可以根据“workplace_type_text”列筛选数据。

import json
import pandas as pd

with open('case_study.json', encoding='utf-8') as file:
    data = json.load(file)

df = pd.DataFrame(data['data'])

filtered_df = df[df['workplace_type_text'] == 'Fully remote']

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)

print(filtered_df)
a6b3iqyw

a6b3iqyw2#

你的data json对象是一个字典,你可以看到它:

type(data)
# dict

data只有一个名为'data'的密钥:

data.keys()
# dict_keys(['data'])

所以你需要通过值来过滤data['data']
要提取所需的项目,请使用filter

result = list(filter(lambda x: x['workplace_type_text'] == 'Fully remote', data['data']))

或者一个简单的列表理解:

result = [job for job in data['data'] if job['workplace_type_text'] == 'Fully remote']

可视化结果,例如:

[(job['title'], job['workplace_type_text']) for job in result] 
# [('Senior Sales Account Executive ', 'Fully remote'),
#  ('Senior Software Developer', 'Fully remote'),
#  ('Customer Success Manager', 'Fully remote'),
#  ('Senior Sales Account Executive ', 'Fully remote'),
#  ('Customer Success Manager', 'Fully remote'),
#  ('Community Marketing Manager', 'Fully remote'),
#  ('Partnerships Marketing Manager', 'Fully remote'),
#  ('Head of Support Engineering (Rails)', 'Fully remote'),
#  ('Head of Support Engineering (Rails)', 'Fully remote'),
#  ('Demand Generation Manager', 'Fully remote'),
#  ('Partnerships Marketing Manager', 'Fully remote')]

相关问题