【问题标题】:Python: How can I go through all the values in this code? [duplicate]Python:我怎样才能遍历这段代码中的所有值? [复制]
【发布时间】:2019-08-21 15:05:49
【问题描述】:

我想从 API 获取信息。 我需要获取这些行的信息:

Lines = ["Piccadilly", "Victoria", "Bakerloo", "Central", "Circle" , "District", "Jubilee", "Metropolitan", "Northern"]

所以我创建了这段代码:

url = 'https://api.tfl.gov.uk/Line/'
tag = '?app_id=XXXXX&app_key=YYYYYYY'

for Line in Lines:
    r = rq.get(url + str(Line) + tag)
    time.sleep(2)
    info = json.loads(r.content)
info

我得到的信息仅适用于 Lines 的最后一个组件,我想将 Lines 中每个组件的信息保存在数据框中。

这是我只为北方得到的

[{'$type': 'Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities',
  'id': 'northern',
  'name': 'Northern',
  'modeName': 'tube',
  'disruptions': [],
  'created': '2019-08-20T16:25:25.35Z',
  'modified': '2019-08-20T16:25:25.35Z',
  'lineStatuses': [],
  'routeSections': [],
  'serviceTypes': [{'$type': 'Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities',
    'name': 'Regular',
    'uri': '/Line/Route?ids=Northern&serviceTypes=Regular'},
   {'$type': 'Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities',
    'name': 'Night',
    'uri': '/Line/Route?ids=Northern&serviceTypes=Night'}],
  'crowding': {'$type': 'Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities'}}]

有什么想法吗?

【问题讨论】:

    标签: python for-loop


    【解决方案1】:

    将信息附加到这样的列表中:

    # create empty list
    info = []
    
    for Line in Lines:
        r = rq.get(url + str(Line) + tag)
        time.sleep(2)
        # append to list
        info.append(json.loads(r.content))
    info
    

    假设您已经创建了一个名为 df 的数据框,您可以使用它将该行插入数据框:

        import pandas as pd
        import json
    
        # put all columns in a list
        columns = ['id', 'name', 'modeName'] # incomplete list
    
        def insert(df, row):
                # get highest index
                insert_loc = df.index.max()
                # insert row
                if pd.isna(insert_loc):
                        df.loc[0] = row
                else:
                        df.loc[insert_loc + 1] = row
    
        for record in info:
                row = []
                for col in columns:
                        row.append(info[col])
                insert(df,row)
    

    【讨论】:

    • 谢谢@J.D.你能告诉我如何在数据框中获得这些结果吗?
    • 我更新了答案
    【解决方案2】:

    您正在重新分配/覆盖info。使用列表或字典等数据结构添加/附加到它。

    info={}
    for Line in Lines:
        r = rq.get(url + str(Line) + tag)
        time.sleep(2)
        info[Line] = json.loads(r.content)
    

    【讨论】:

    • 谢谢@Ricky Kim,你能告诉我如何在数据框中获得这些结果吗?
    【解决方案3】:

    你可以这样做

    url = 'https://api.tfl.gov.uk/Line/'
    tag = '?app_id=XXXXX&app_key=YYYYYYY'
    
    info={}
    for Line in Lines:
        r = rq.get(url + str(Line) + tag)
        time.sleep(2)
        info[Line]=r.json()
    print(info)
    

    【讨论】:

    • 谢谢,如何将结果保存在数据框中?
    • @Alexa 您返回的 json 并不完全是“数据帧就绪”。您需要先进行一些清洁或移动物品,然后才能轻松做到这一点。而你如何做到这一点取决于你所追求的信息。
    • 我想在我的数据框中获取这些列:'id': 'northern', 'name': 'Northern', 'modeName': 'tube', 'disruptions': [], '创建':'2019-08-20T16:25:25.35Z','修改':'2019-08-20T16:25:25.35Z','lineStatuses':[],'routeSections':[],'serviceTypes' : [{'$type': 'Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities', 'name': 'Regular', 'uri': '/Line/Route?ids=Northern&serviceTypes=常规'}我尝试了一个 for 循环,但它不起作用,你能帮我@SuperStew吗?
    【解决方案4】:

    您在循环中的每次迭代中都覆盖了信息。一种方法是推动每个 在列表中返回 json。

    url = 'https://api.tfl.gov.uk/Line/'
    tag = '?app_id=XXXXX&app_key=YYYYYYY'
    infos = []
    
    for Line in Lines:
        r = rq.get(url + str(Line) + tag)
        time.sleep(2)
        infos.append(json.loads(r.content))
    
    print(infos)
    

    【讨论】:

    • 谢谢@Rambarun Komaljeet,你能告诉我如何在数据框中获得这些结果吗?
    猜你喜欢
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多