将解析后的csv文件作为字符串自动转换为pandas数据帧

时间:2017-04-07 20:21:08

标签: python csv pandas

我正在尝试从mysportsfeeds.com获取一些数据,它会返回一个csv文件,该文件会立即转换为python中的字符串。

import requests
import base64


username = 'XXXX'
password = 'XXXX'


def send_request(request):
    # Request

    try:
        response = requests.get(
        url=request,
        params={
            "fordate": "20170401"
        },
        headers={
            "Authorization": "Basic " + base64.b64encode(username + ":" + password)
        }
    )
    return response.content
except requests.exceptions.RequestException:
        print('HTTP Request failed')

res_table = send_request('https://www.mysportsfeeds.com/api/feed/pull/nba/2016-2017-regular/daily_game_schedule.csv?fordate=20170407')

如何以最快的方式将res_table转换为pandas dataFrame

它看起来像:

#Date/Time of Update: (none),#Game ID,#Game Date,#Game Time,#Unplayed,#In Progress,#Completed,#Current Quarter,#Current Quarter Seconds Remaining,#Current Intermission,#Away Team ID,#Away Team Abbr.,#Away Team City,#Away Team Name,#Home Team ID,#Home Team Abbr.,#Home Team City,#Home Team Name,#Location,#Away Score,#Home Score
,35119,2017-04-07,7:30PM,true,false,false,,,,91,ATL,Atlanta,Hawks,86,CLE,Cleveland,Cavaliers,Quicken Loans Arena
,35120,2017-04-07,7:30PM,true,false,false,,,,92,MIA,Miami,Heat,81,TOR,Toronto,Raptors,Air Canada Centre
,35121,2017-04-07,8:00PM,true,false,false,,,,83,NYK,New York,Knicks,107,MEM,Memphis,Grizzlies,FedEx Forum
,35122,2017-04-07,8:00PM,true,false,false,,,,88,DET,Detroit,Pistons,109,HOU,Houston,Rockets,Toyota Center
,35123,2017-04-07,8:30PM,true,false,false,,,,106,SAS,San Antonio,Spurs,108,DAL,Dallas,Mavericks,American Airlines Center
,35124,2017-04-07,9:00PM,true,false,false,,,,110,NOP,New Orleans,Pelicans,99,DEN,Denver,Nuggets,Pepsi Center
,35125,2017-04-07,9:00PM,true,false,false,,,,100,MIN,Minnesota,Timberwolves,98,UTA,Utah,Jazz,Vivint Smart Home Arena
,35126,2017-04-07,10:00PM,true,false,false,,,,96,OKL,Oklahoma City,Thunder,104,PHX,Phoenix,Suns,Talking Stick Resort Arena
,35127,2017-04-07,10:30PM,true,false,false,,,,103,SAC,Sacramento,Kings,105,LAL,Los Angeles,Lakers,Staples Center

1 个答案:

答案 0 :(得分:2)

您似乎需要StringIO

from pandas.compat import StringIO

df = pd.read_csv(StringIO(res_table))