在 Python 匹配值数组中迭代 JSON 数组

zaboy618

我正在使用此处找到Songkick api,并且非常接近完成我正在处理的程序以获取有关一些特定艺术家即将举行的演出的信息。我已经输入了一个数组metro_areas,我想跟踪和输出这些城市及其随附的 ID 内的显示。我正在提取的其他信息是演出日期、艺术家姓名、场地名称。基本上现在我的程序能够从artist_ids我输入的列表中提取每个节目,遍历请求 url 中的 id 以获取参数中的日期范围。我当前的输出看起来像这样:

['日期', '艺术家姓名', '场地名称', '城市', 'metroArea ID']

['FEB - 7', 'Rosalia', 'NOTO', 'Philadelphia, PA, US', 5202]
['FEB - 8', 'Rosalia', 'Audio', 'San Francisco, CA, US', 26330]
['FEB - 8', 'Kid Cudi', 'Shady Park', 'Tempe, AZ, US', 23068]
['FEB - 8', 'Kid Cudi', 'Madison Square Garden', 'New York City, NY, US', 7644]

我希望输出是这样的:

['FEB - 8', 'Rosalia', 'Audio', 'San Francisco, CA, US', 26330]
['FEB - 8', 'Kid Cudi', 'Madison Square Garden', 'New York City, NY, US', 7644]

基于这个数组,我在程序开始时定义了与 songkick 对象数组中的 Metro_areas 匹配。

metro_areas = [
    ('Los Angeles', '17835'),
    ('San Francisco', '26330'),
    ('New York City', '7644'),
    ('Seattle', '2846'),
    ('Nashville', '11104')
]

这是我为每个 Artist_id 提取的 json 对象数组:

{
  "resultsPage": {
    "results": {
      "event": [
        {
          "id":11129128,
          "type":"Concert",
          "uri":"http://www.songkick.com/concerts/11129128-wild-flag-at-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
          "displayName":"Wild Flag at The Fillmore (April 18, 2012)",
          "start": {
            "time":"20:00:00",
            "date":"2012-04-18",
            "datetime":"2012-04-18T20:00:00-0800"
          },
          "performance": [
            {
              "artist": {
                "id":29835,
                "uri":"http://www.songkick.com/artists/29835-wild-flag?utm_source=PARTNER_ID&utm_medium=partner",
                "displayName":"Wild Flag",
                "identifier": []
              },
              "id":21579303,
              "displayName":"Wild Flag",
              "billingIndex":1,
              "billing":"headline"
            }
          ],
          "location": {
            "city":"San Francisco, CA, US",
            "lng":-122.4332937,
            "lat":37.7842398
          },
          "venue": {
            "id":6239,
            "displayName":"The Fillmore",
            "uri":"http://www.songkick.com/venues/6239-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
            "lng":-122.4332937,
            "lat":37.7842398,
            "metroArea": {
              "id":26330,
              "uri":"http://www.songkick.com/metro-areas/26330-us-sf-bay-area?utm_source=PARTNER_ID&utm_medium=partner",
              "displayName":"SF Bay Area",
              "country": { "displayName":"US" },
              "state": { "displayName":"CA" }
            }
          },
          "status":"ok",
          "popularity":0.012763
        }, ....
      ]
    },
    "totalEntries":24,
    "perPage":50,
    "page":1,
    "status":"ok"
  }
}

一些更多的代码来看看我是如何从 Songkick 请求中的 JSON 获得我的输出的。

metro_areas = [
                    ('Los Angeles','17835'),
                    ('San Francisco', '26330'),
                    ('New York City','7644'),
                    ('Seattle','2846'),
                    ('Nashville','11104')
               ]



# artists we want to track
artist_ids = [
    ('Rosalia', '4610868'), ('EARTHGANG', '5720759'), ('Kid Cudi', '8630279'), ('Kanye West', '5566863'),
    ('Ludacris', '398291'), ('Hayley Williams', '10087966')
]

# Fetch existing events in each metro area
for artist_id in artist_ids:

params = {
        'apikey': 'API_KEY',
        'min_date': '2020-02-01',
        'max_date': '2020-02-08',
        # 'type': 'Concert'
    }

r = requests.get('https://api.songkick.com/api/3.0/artists/' + artist_id[1] + '/calendar.json', params=params)
    response = r.json()

shows = response['resultsPage']['results']

    for show in shows:
        try:
            shows = shows['event']

            formatted_shows = [{
                'artistID': [perf['artist']['id'] for perf in s['performance']],
                'date': s['start']['date'],
                'name': [perf['artist']['displayName'] for perf in s['performance']],
                'metroArea': s['venue']['metroArea']['id'],
                'city': s['location']['city'],
                'venue': s['venue']['displayName']
                }
                for s in shows if len(s['performance']) > 0
            ]
            for sub in formatted_shows:
                if sub['artistID'] == artist_id[1]:
                    sub['name'] = artist_id[0]
new_show = artist_id[1]
                new_show_name = artist_id[0]
                new_date = sub['date']
                new_date_time = new_date = datetime.strptime(new_date, '%Y-%m-%d')
                date_time_fin = new_date_time.strftime('%b - %-d').upper()

                formatted_show_final = [date_time_fin, new_show_name, sub['venue'], sub['city'], sub['metroArea']
                print(formatted_show_final)


长话短说,我需要找到一种方法来遍历我列出的每个 Metro_areas id(洛杉矶、旧金山、纽约、西雅图、纳什维尔),并且只输出与'metroArea': s['venue']['metroArea']['id']每个请求迭代匹配的节目

手表

如果我很好地理解了这个问题,请在第二个 for 循环中添加: if sub['metroArea'] in [area[1] for area in metro_areas]:

    for show in shows:
        try:
            shows = shows['event']
            formatted_shows = [{
                'artistID': [perf['artist']['id'] for perf in s['performance']],
                'date': s['start']['date'],
                'name': [perf['artist']['displayName'] for perf in s['performance']],
                'metroArea': s['venue']['metroArea']['id'],
                'city': s['location']['city'],
                'venue': s['venue']['displayName']
                }
                for s in shows if len(s['performance']) > 0
            ]
            for sub in formatted_shows:
                #Modified here to apply str() function to transform #sub['metroArea'] to string instead of int value
                if str(sub['metroArea']) in [area[1] for area in metro_areas]:
                    if sub['artistID'] == artist_id[1]:
                        sub['name'] = artist_id[0]
                    new_show = artist_id[1]
                    new_show_name = artist_id[0]
                    new_date = sub['date']
                    new_date_time = new_date = datetime.strptime(new_date, '%Y-%m-%d')
                    date_time_fin = new_date_time.strftime('%b - %-d').upper()
                    formatted_show_final = [date_time_fin, new_show_name, sub['venue'], sub['city'], sub['metroArea']]
                    print(formatted_show_final)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章