ValueError: Invalid file path or buffer object type: <class 'dict'> python

20,799

You can try this,

import requests as rq
import pandas as pd
import json

dfs = []

with open('tfl_list_stops.txt', encoding='utf-8') as file:
    file_list = [line.strip() for line in file]

for ids in file_list:
    url = 'https://api.tfl.gov.uk/StopPoint/' + ids + '?includeCrowdingData=true'
    r = rq.get(url)

    content = json.loads(r.text)
    dfs.append(pd.DataFrame([content]))

df = pd.concat(dfs, ignore_index=True, sort=False)
df.to_csv('stop_loc.csv')

print(df)

                                               $type     naptanId  \
0  Tfl.Api.Presentation.Entities.StopPoint, Tfl.A...  940GZZLUBMY   

         modes  icsCode smsCode            stopType stationNaptan  \
0  [bus, tube]  1000021   77031  NaptanMetroStation   940GZZLUBMY   

                                               lines  \
0  [{'$type': 'Tfl.Api.Presentation.Entities.Iden...   

                                           lineGroup  \
0  [{'$type': 'Tfl.Api.Presentation.Entities.Line...   

                                      lineModeGroups  status           id  \
0  [{'$type': 'Tfl.Api.Presentation.Entities.Line...    True  940GZZLUBMY   

                       commonName  placeType  \
0  Bermondsey Underground Station  StopPoint   

                                additionalProperties  \
0  [{'$type': 'Tfl.Api.Presentation.Entities.Addi...   

                                            children       lat       lon  
0  [{'$type': 'Tfl.Api.Presentation.Entities.Stop...  51.49775 -0.063993 
Share:
20,799
istanbul34
Author by

istanbul34

Updated on July 09, 2022

Comments

  • istanbul34
    istanbul34 almost 2 years

    When running the below code I'm getting the following error:

    ValueError: Invalid file path or buffer object type: class 'dict'>

    code:

    import requests as rq
    import pandas as pd
    import json
    
    df = pd.DataFrame()
    temp = pd.DataFrame()
    with open("tfl_list_stops.txt", encoding="utf-8") as file:
        fileList = [line.strip() for line in file]
    for ids in fileList:
        r = rq.get('https://api.tfl.gov.uk/StopPoint/' + ids + '?includeCrowdingData=true')
        r = r.text
        content = json.loads(r)
        temp = pd.read_json(content)
        df = pd.concat([df, temp], axis=1)
    df.to_csv('stop_loc.csv')
    
    • Flux
      Flux about 4 years
      My wild guess: remove content = json.loads(r) and change the next line to temp = pd.read_json(r).
    • Ethan Wicker
      Ethan Wicker about 3 years
      The above comment by @Flux solved the issue for me.