【问题标题】:Create Tuples out of JSON File从 JSON 文件创建元组
【发布时间】:2018-02-02 17:36:10
【问题描述】:

我一直致力于解析 JSON 文件以使其更易于在 PostgreSQL 中使用,并且想知道将 JSON 字典解析为元组的最佳方法是什么?

例如,两个变量的行如下所示:

第一个

"attributes": {"RestaurantsTableService": false, "GoodForMeal": {"dessert": false, "latenight": false, "lunch": false, "dinner": false, "breakfast": false, "brunch": false}, "Alcohol": "none", "Caters": true, "HasTV": false, "RestaurantsGoodForGroups": true, "NoiseLevel": "quiet", "WiFi": "no", "RestaurantsAttire": "casual", "RestaurantsReservations": false, "OutdoorSeating": false, "BusinessAcceptsCreditCards": true, "RestaurantsPriceRange2": 1, "BikeParking": true, "RestaurantsDelivery": false, "Ambience": {"romantic": false, "intimate": false, "classy": false, "hipster": false, "divey": false, "touristy": false, "trendy": false, "upscale": false, "casual": false}, "RestaurantsTakeOut": true, "GoodForKids": true, "BusinessParking": {"garage": false, "street": false, "validated": false, "lot": false, "valet": false}}

第二个:

"hours": {"Monday": "7:30-22:00", "Tuesday": "7:30-22:00", "Friday": "7:30-22:00", "Wednesday": "7:30-22:00", "Thursday": "7:30-22:00", "Sunday": "7:30-21:00", "Saturday": "7:30-22:00"}

我希望它们采用这种格式:

对于属性:

Attributes: [(RestaurantsTableService, False)(dessert, False)(latenight, False)(lunch, False)(dinner, False)(breakfast, False)(brunch, False)(Alcohol, none)(Caters, True)(HasTV, False)(RestaurantsGoodForGroups, True)(NoiseLevel, quiet)(WiFi, no)(RestaurantsAttire, casual)(RestaurantsReservations, False)(OutdoorSeating, False)(BusinessAcceptsCreditCards, True)(RestaurantsPriceRange2, 1)(BikeParking, True)(RestaurantsDelivery, False)(romantic, False)(intimate, False)(classy, False)(hipster, False)(divey, False)(touristy, False)(trendy, False)(upscale, False)(casual, False)(RestaurantsTakeOut, True)(GoodForKids, True)(garage, False)(street, False)(validated, False)(lot, False)(valet, False)]

小时数:

Hours: [(Friday, 9:00,12:00)(Tuesday, 14:00,19:00)(Thursday, 14:00,19:00)(Wednesday, 14:30,17:00)(Monday, 14:30,17:00)]

到目前为止,这是我的代码。现在我一直在尝试访问变量中每个字典的值。我能够遍历它们,但无法访问布尔值、整数或字符串值。

import json
import ast
import pandas as pd
from datetime import datetime
from collections import OrderedDict, defaultdict

def cleanStr4SQL(s):
    return s.replace("'","`").replace("\n"," ")


def parseBusinessData():
    #read the JSON file
    with open('yelp_business.JSON','r') as f:  #Assumes that the data files are available in the current director. If not, you should set the path for the yelp data files.  
        outfile =  open('business.txt', 'w')
        line = f.readline()
        count_line = 0
        #read each JSON abject and extract data
        while line:
            data = json.loads(line)


#            jsondict = ast.literal_eval(str(data))
#            df = pd.DataFrame(jsondict['attributes'])
#            df['features'] = df.index.str.rjust(5, '0')
#            df['atts'] = df['features'].apply(attributes)
#            outfile.write(str([item for item in df['atts']]) + '\t')
#            df = df.apply(attributes)
#            print(str([item for item in data['attributes']]))
            outfile.write(str([k for k in [item for item in [l for l in data['attributes']]]]) + '\t') # write your own code to process attributes
            outfile.write(str([item for item in data['hours']]) + '\t') # write your own code to process hours
            outfile.write('\n');

            line = f.readline()
            count_line +=1
    print(count_line)
    outfile.close()
    f.close()

def attributes(val):
    if val == False:
        return 0
    if val == True:
        return 1

如果您有任何其他问题或疑虑,请告诉我。任何建议表示赞赏。

感谢您的阅读。

【问题讨论】:

  • 处理字典时,您在遍历字典时隐式访问键,与dict.keys() 相同。您可以尝试 dict.values() 仅用于值或 dict.iteritems() 用于键值对。
  • 在您的第一个 JSON 中,有类似 GoodForMeal: {dessert:false} 的内容,它不会出现在您的预期结果中。这是故意的吗?
  • 不,我很抱歉,这不是故意的。我当场做了这些例子,主要是为了举例说明我想如何重新格式化数据。对于任何混淆,我深表歉意。

标签: python json postgresql parsing formatting


【解决方案1】:

据我了解,您似乎希望生成特定格式的字符串,从而展平嵌套字典中的属性。

这是你想要的吗?

import json

def flatten(D,key):
    L = []
    for k,v in D[key].items():
        if isinstance(v,dict):
            for kk,vv in v.items():
                L.append((kk,vv))
        else:
            L.append((k,v))
    return L

att_json = '{"attributes": {"RestaurantsTableService": false, "GoodForMeal": {"dessert": false, "latenight": false, "lunch": false, "dinner": false, "breakfast": false, "brunch": false}, "Alcohol": "none", "Caters": true, "HasTV": false, "RestaurantsGoodForGroups": true, "NoiseLevel": "quiet", "WiFi": "no", "RestaurantsAttire": "casual", "RestaurantsReservations": false, "OutdoorSeating": false, "BusinessAcceptsCreditCards": true, "RestaurantsPriceRange2": 1, "BikeParking": true, "RestaurantsDelivery": false, "Ambience": {"romantic": false, "intimate": false, "classy": false, "hipster": false, "divey": false, "touristy": false, "trendy": false, "upscale": false, "casual": false}, "RestaurantsTakeOut": true, "GoodForKids": true, "BusinessParking": {"garage": false, "street": false, "validated": false, "lot": false, "valet": false}}}'
att = json.loads(att_json)
att_list = flatten(att,'attributes')
s = 'Attributes: [' + ''.join(['({}, {})'.format(k,v) for k,v in att_list]) + ']'
print(s)

hours_json = '{"hours": {"Monday": "7:30-22:00", "Tuesday": "7:30-22:00", "Friday": "7:30-22:00", "Wednesday": "7:30-22:00", "Thursday": "7:30-22:00", "Sunday": "7:30-21:00", "Saturday": "7:30-22:00"}}'
hours = json.loads(hours_json)
hours_list = flatten(hours,'hours')
s = 'Hours: [' + ''.join(['({}, {})'.format(k,v.replace('-',',')) for k,v in hours_list]) + ']'
print(s)

输出:

Attributes: [(RestaurantsTableService, False)(dessert, False)(latenight, False)(lunch, False)(dinner, False)(breakfast, False)(brunch, False)(Alcohol, none)(Caters, True)(HasTV, False)(RestaurantsGoodForGroups, True)(NoiseLevel, quiet)(WiFi, no)(RestaurantsAttire, casual)(RestaurantsReservations, False)(OutdoorSeating, False)(BusinessAcceptsCreditCards, True)(RestaurantsPriceRange2, 1)(BikeParking, True)(RestaurantsDelivery, False)(romantic, False)(intimate, False)(classy, False)(hipster, False)(divey, False)(touristy, False)(trendy, False)(upscale, False)(casual, False)(RestaurantsTakeOut, True)(GoodForKids, True)(garage, False)(street, False)(validated, False)(lot, False)(valet, False)]
Hours: [(Monday, 7:30,22:00)(Tuesday, 7:30,22:00)(Friday, 7:30,22:00)(Wednesday, 7:30,22:00)(Thursday, 7:30,22:00)(Sunday, 7:30,21:00)(Saturday, 7:30,22:00)]

【讨论】:

  • 太棒了!它工作得很好。感谢您的帮助!不过,有一个简单的问题,您能向我解释一下您是如何提出 flatten 功能的吗?
  • @mrsquid 嵌套字典只有两层。第一个 for 遍历顶层,如果键的值是字典,它会遍历该字典的键/值。它基本上删除了每个嵌套字典的键名,并将嵌套字典的键/值附加到与更高级别的键/值相同的列表中。
  • 啊,我明白了!谢谢你的解释!
猜你喜欢
  • 2017-01-04
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多