Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:我如何遍历代码中的所有值?_Python_For Loop - Fatal编程技术网

Python:我如何遍历代码中的所有值?

Python:我如何遍历代码中的所有值?,python,for-loop,Python,For Loop,我想从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

我想从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
我得到的信息只针对行的最后一个组件,我想将行中每个组件的信息保存在数据帧中

这是我只为Northern买的

[{'$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'}}]

有什么想法吗?

将信息添加到如下列表中:

# 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)

您正在重新分配/覆盖
info
。使用数据结构(如列表或字典)向其添加/追加

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

在循环中的每次迭代中,您都会覆盖信息。一种方法是推动每一个 在列表中返回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)

你可以这样做

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)

谢谢@Ricky Kim,你能告诉我如何在数据框中获得论文结果吗?谢谢@J.D.你能告诉我如何在数据框中获得论文结果吗?我更新了答案谢谢@Rambarun Komaljeet,您能告诉我如何将这些结果保存在数据框中吗?谢谢,我如何将结果保存在数据框中?@Alexa您得到的json并不是完全“数据框就绪”。你需要做一点清洁或移动东西,然后才能轻松做到这一点。我想在我的数据框中获得以下列:“id”:“northern”,“name”:“northern”,“modeName”:“tube”,“disruptions”:[],“created”:“2019-08-20T16:25:25.35Z”,“modified”:“2019-08-20T16:25:25.35Z”,“LineStatus”:[],“routeSections”:[],“serviceTypes”:[{“$type”:“Tfl.Api.Presentation.Entities.LineServiceTypeInfo,Tfl.Api.Presentation.Entities”,“name”:“Regular”,“uri”:“/Line/Route?ids=Northern&serviceTypes=Regular”}我尝试使用for循环,但不起作用,你能帮助我@supersew吗?