Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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')
like image 870
istanbul34 Avatar asked Oct 17 '25 15:10

istanbul34


1 Answers

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 
like image 163
E. Zeytinci Avatar answered Oct 19 '25 05:10

E. Zeytinci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!