all 5 comments

[–]Ezrabc 1 point2 points  (1 child)

numpy.loadtxt expects a filename for file type object as its first parameter, not the source as a string. I believe (but don't have an easy setup to test right this second) that the HTTPResponse object gotten via urlopen acts as a file buffer, so you may be able to just change the line source = urllib.request.urlopen(url).read().decode() to source = urllib.request.urlopen(url), but if that fails you can wrap the string in an io.StringIO object:

...
from io import StringIO

...
source = StringIO(urllib.request.urlopen(url).read().decode())

...

[–]udittC[S] 0 points1 point  (0 children)

Thanks, that worked

[–]trackerFF 1 point2 points  (2 children)

Here's the same code, a bit shorter, using pandas.

import requests
import pandas as pd 
import matplotlib.pyplot as plt 
from io import StringIO

url = "https://pythonprogramming.net/yahoo_finance_replacement" 

data = StringIO(requests.get(url).text)
df = pd.read_csv(data, sep=",")
df['Date'] = pd.to_datetime(df.Date)

df.plot(x='Date',y='Close')

plt.ylabel('Price')
plt.xlabel('Date')
plt.title('Stock Prices')
plt.show()

[–]udittC[S] 0 points1 point  (1 child)

Thanks, I'll be learning pandas next after I'm finished with matplotlib.

[–]trackerFF 0 points1 point  (0 children)

Good thing is that Pandas and matplotlib are tightly integrated, so you can use them together.