我正在尝试将json对象作为输入并查找符合特定条件的项目数。 json对象结构位于嵌套的python字典中,例如:
businesses= ["{\"hours\":
{
\"tuesday\": [[\"11:30\", \"23:00\"]],
\"thursday\": [[\"11:30\", \"23:00\"]],
\"wednesday\": [[\"11:30\", \"23:00\"]],
\"friday\": [[\"11:30\", \"23:00\"]],
\"sunday\": [[\"9:00\", \"23:00\"]],
\"monday\": [[\"11:30\", \"23:00\"]],
\"saturday\": [[\"11:30\", \"23:00\"]]
},
\"name\": \"XYZ\"
}"]
该结构中将有多个项目。我遇到的问题是编写for循环以进入各个级别,并搜索在上午10点之前的周日营业的公司。
是这样的:
def count_businesses(object):
for i in object:
for j in i:
....
但是当我达到那个水平时,它似乎输出了字典中的每个字母。另外,我不确定如何编写函数来查找打开日期和时间,对我来说,必须在上午10点之前找到星期日,然后返回一个计数。时间在该对象的字典内的数组中,如图所示。
任何帮助将不胜感激!
答案 0 :(得分:1)
似乎什么是python字典之间有些混淆。 businesses
中数组中的数据实际上是JavaScript Object Notation(JSON)中的字符串,而python将其视为字符串。为了将其用作python字典,您将需要使用python的json
库对其进行转换。转换看起来像
import json
python_obj = json.loads(json_str)
您提供的对象是JSON字符串数组,例如
businesses = ["{\"hours\":"
"{"
"\"tuesday\": [[\"11:30\", \"23:00\"]],"
"\"thursday\": [[\"11:30\", \"23:00\"]],"
"\"wednesday\": [[\"11:30\", \"23:00\"]],"
"\"friday\": [[\"11:30\", \"23:00\"]],"
"\"sunday\": [[\"9:00\", \"23:00\"]],"
"\"monday\": [[\"11:30\", \"23:00\"]],"
"\"saturday\": [[\"11:30\", \"23:00\"]]"
"},"
"\"name\": \"XYZ\""
"}"]
一个python字典数组看起来像下面的
businesses = [
{
"hours":{
"tuesday":[["11:30","23:00"]],
"thursday":[["11:30","23:00"]],
"wednesday":[["11:30","23:00"]],
"friday":[["11:30", "23:00"]],
"sunday":[["9:00", "23:00"]],
"monday":[["11:30", "23:00"]],
"saturday":[["11:30", "23:00"]]
},
"name":"XYZ"
}
]
因此,看到它输出每个字母的原因是因为您正在遍历字符串,而不是python字典。当python遍历字符串时,它将遍历每个字符。就像下面这样。
string_data = "123456789"
# will iterate through each character
for i in string_data:
print(i) # will print 9 times each time outputting a character in order
对于函数,您将需要确保在进行时间比较时,您正在使用python时间对象而不是字符串,因为那样可以准确地比较时间。我不完全确定为什么时间会以嵌套数组(例如[["11:30","23:00"]]
)的形式列出,因此,如果其他业务的数据格式不同,您可能需要修改以下函数。
这是一个描述您需要的功能。
import json, datetime
businesses = ["{\"hours\":"
"{"
"\"tuesday\": [[\"11:30\", \"23:00\"]],"
"\"thursday\": [[\"11:30\", \"23:00\"]],"
"\"wednesday\": [[\"11:30\", \"23:00\"]],"
"\"friday\": [[\"11:30\", \"23:00\"]],"
"\"sunday\": [[\"9:00\", \"23:00\"]],"
"\"monday\": [[\"11:30\", \"23:00\"]],"
"\"saturday\": [[\"11:30\", \"23:00\"]]"
"},"
"\"name\": \"XYZ\""
"}"]
def count_businesses(business_list):
"""
:param business_list: An array of business in JSON to query from
:return: Int of the count of businesses that are open on Sunday before 10 am
"""
# initialize the array that will contain the businesses that meet the search criteria
businesses_found = []
# python time object of 10:00am that will be used to check against
opening_business_time = datetime.time(hour=10)
# iterate through each busineses to check if it meets the search criteria
for business in business_list:
# since each business is in JSON, we convert it into a Python object
business_obj = json.loads(business)
# Look into the 'hours' key, then the 'sunday' key and get the first item in the array. ( i.e ["11:30","23:00"])
sunday_hours = business_obj["hours"]["sunday"][0]
# read in the sunday opening hours as a string from the first value of the array. {i.e "11:30")
sunday_opening_hours_str = sunday_hours[0]
# convert the sunday opening hours into a time object so it can be compared.
# '%H:%M' looks for the format HH:MM in a string.
# for more reference. https://docs.python.org/3.6/library/datetime.html#strftime-and-strptime-behavior
sunday_opening_hours_time = datetime.datetime.strptime(sunday_opening_hours_str, '%H:%M').time()
# if sunday opening hours is before 10 am
if sunday_opening_hours_time < opening_business_time:
# add the business object to the list
businesses_found.append(business_obj)
# returns the count of the businesses that met the search criteria
return len(businesses_found)
total = count_businesses(businesses)
print(total)
答案 1 :(得分:1)
将其添加为星期日的空检查条件。
if(business_obj [“ hours”]中的“星期日”)