pythondata-import

import web based .txt file into python


I think this is simple but I am not finding an answer that works. The data importing seems to work but separating the "/" numbers doesnt code is below. thanks for the help.

import urllib.request
opener = urllib.request.FancyURLopener({})
url = "http://jse.amstat.org/v22n1/kopcso/BeefDemand.txt"
f = opener.open(url)
content = f.read()
# below are the 3 different ways I tried to separate the data
content.encode('string-escape').split("\\x")
content.split('\r')
content.split('\\') 

Solution

  • I highly recommend Pandas for reading and analysing this kind of file. It supports reading directly from a url and also gives meaningful analysis ability.

    import pandas
    url = "http://jse.amstat.org/v22n1/kopcso/BeefDemand.txt"
    
    df = pandas.read_table(url, sep="\t+", engine='python', index_col="Year")
    

    Note that you have multiple repeated tabs as separators in that file, which is handled by the sep="\t+". The repeats also means you have to use the python engine.

    Now that the file is read into a dataframe, we can do easy plotting for instance:

    df[['ChickPrice', 'BeefPrice']].plot()
    

    plot resut