2017-07-27 90 views
0

我使用urlopen獲取數據的字符串作爲follows.I希望將字符串轉換爲數據幀和儲備幾列,樣態,AQI等。我不知道該怎麼做,並想向你尋求建議。謝謝!如何將字符串轉換成數據幀在Python

response=urlopen(URL).read().decode('utf-8') 
    print(response) 
"DateIssue","DateForecast","ReportingArea","StateCode","Latitude","Longitude","ParameterName","AQI","CategoryNumber","CategoryName","ActionDay","Discussion" 
"2017-05-01 ","2017-05-01 ","Metropolitan Washington","DC","38.919","-77.013","O3","42","1","Good","false","" 
"2017-05-01 ","2017-05-01 ","Metropolitan Washington","DC","38.919","-77.013","PM2.5","46","1","Good","false","" 
"2017-05-01 ","2017-05-02 ","Metropolitan Washington","DC","38.919","-77.013","O3","44","1","Good","false","" 
"2017-05-01 ","2017-05-02 ","Metropolitan Washington","DC","38.919","-77.013","PM2.5","25","1","Good","false","" 
"2017-05-01 ","2017-05-03 ","Metropolitan Washington","DC","38.919","-77.013","O3","44","1","Good","false","" 
"2017-05-01 ","2017-05-03 ","Metropolitan Washington","DC","38.919","-77.013","PM2.5","25","1","Good","false","" 
"2017-05-01 ","2017-05-04 ","Metropolitan Washington","DC","38.919","-77.013","O3","42","1","Good","false","" 
"2017-05-01 ","2017-05-04 ","Metropolitan Washington","DC","38.919","-77.013","PM2.5","29","1","Good","false","" 

回答

3

看來你可以使用:

from pandas.compat import StringIO 
df = pd.read_csv(StringIO(response)) 

但也許也適用:

df = read_csv(URL) 
1

使用read_fwfto_csv()然後read_csv()

import io 
import pandas as pd 

df = pd.read_fwf(io.StringIO(response)) 

df.to_csv('data.csv') 

result_df = pd.read_csv('data.csv',) 
相關問題