Convert text data from requests object to dataframe with pandas

后端 未结 4 952
离开以前
离开以前 2021-02-05 10:31

Using requests I am creating an object which is in .csv format. How can I then write that object to a DataFrame with pandas?

To get the requests object in text format:

4条回答
  •  梦毁少年i
    2021-02-05 10:38

    I think you can use read_csv with url:

    pd.read_csv(url)
    

    filepath_or_buffer : str, pathlib.Path, py._path.local.LocalPath or any object with a read() method (such as a file handle or StringIO)

    The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.csv

    import pandas as pd
    import io
    import requests
    
    url = r'http://...' 
    r = requests.get(url)  
    df = pd.read_csv(io.StringIO(r))
    

    If it doesnt work, try update last line:

    import pandas as pd
    import io
    import requests
    
    url = r'http://...' 
    r = requests.get(url)  
    df = pd.read_csv(io.StringIO(r.text))
    

提交回复
热议问题