簡體   English   中英

ValueError:無效的文件路徑或緩沖區對象類型:<class 'dict'> Python

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

運行以下代碼時,出現以下錯誤:

ValueError:無效的文件路徑或緩沖區對象類型:類“dict”>

代碼:

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

你可以試試這個

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM