2017-04-07 53 views
1

我想從mysportsfeeds.com獲取一些數據,並且它會返回一個csv文件,該文件會立即轉換爲python中的字符串。將分析後的csv文件自動轉換爲字符串讀取爲熊貓數據幀

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_tablepandasdataFrame最快方法是什麼?

它看起來像:

#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 

回答

2

看來你需要StringIO

from pandas.compat import StringIO 

df = pd.read_csv(StringIO(res_table)) 
+0

是它的工作原理,謝謝。 –

+0

很高興能幫到你! – jezrael

相關問題