Hi Forum,
I have some code written in version 2.7 and I'm running 3.7 on Windows.
Maybe someone can help me with the changes I'd need to make to get the below code to run?
The code and the error I'm getting is below, can anyone help with this?
import urllib2
import time
stockToPull = 'AAPL'
def pullData(stock):
try:
fileLine = stock+' .txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine) == 6:
if 'values' not in eachLine:
saveFile = open(fileLine,'a')
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
print 'Pulled',stock
print 'sleeping'
time.sleep(5)
except Exception,e:
print 'main loop' str(e)
pullData(stockToPull)
--------------------------------------------------------------------------------
This is the error:
File "D:/Users/xxxxx/Desktop/PyChart_1.py", line 27
print 'Pulled',stock
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Pulled',stock)?
I don't know if I can write anything any more clear than the error message you're getting.
print()
is a function in python3, so it's a syntax error to try to use it without parenthases. The error message even suggests a corrected version that'd work.
Or you could use the 2to3 utility which could do all the conversion for you:
https://docs.python.org/3.7/library/2to3.html
Thanks nilamo,
I've tried a few things with the print function but I still get the same error?
print(Pulled,stock)
^
SyntaxError: invalid syntax
That's valid code. Please share the entire traceback, not just part of the error.
>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> Pulled = "first str"
>>> stock = "second str"
>>> print(Pulled,stock)
first str second str
Ok, here's the whole query & the whole error, I've changed it back to original '2' version.
Thanks for any insights you can give me.
import urllib2
import time
stockToPull = 'AAPL'
def pullData(stock):
try:
fileLine = stock+' .txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine) == 6:
if 'values' not in eachLine:
saveFile = open(fileLine,'a')
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
print 'Pulled',stock
print 'sleeping'
time.sleep(5)
except Exception,e:
print 'main loop' str(e)
pullData(stockToPull)
----------------------------------------------------------------------
Error is;
Python 3.7.1 (default, Dec 10 2018, 22:09:34) [MSC v.1915 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 7.2.0 -- An enhanced Interactive Python.
runfile('D:/Users/xxxxxxx/Desktop/PyChart_1.py', wdir='D:/Users/xxxxxxx/Desktop')
Traceback (most recent call last):
File "C:\Users\xxxxxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-1-337dea49fd42>", line 1, in <module>
runfile('D:/Users/xxxxxxx/Desktop/PyChart_1.py', wdir='D:/Users/xxxxxxx/Desktop')
File "C:\Users\xxxxxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\xxxxxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/Users/xxxxxxx/Desktop/PyChart_1.py", line 27
print 'Pulled',stock
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Pulled',stock)?
There is no point covert that code,because the old Yahoo finance iChart API is gone for good.
There several packages or API that can be used for this in python.
import pandas_datareader.data as web
aapl = web.get_quote_yahoo('AAPL')
print(aapl)
Output:
language region quoteType quoteSourceName currency exchange \
AAPL en-US US EQUITY Nasdaq Real Time Price USD NMS
epsTrailingTwelveMonths market marketState exchangeDataDelayedBy \
AAPL 11.91 us_market POST 0
... earningsTimestamp earningsTimestampStart earningsTimestampEnd \
AAPL ... 1548795600 1548882000 1549314000
trailingAnnualDividendRate trailingPE trailingAnnualDividendYield \
AAPL 2.72 12.420654 0.018346
shortName esgPopulated tradeable price
AAPL Apple Inc. False True 147.93
[1 rows x 69 columns]
With 69 columns do this look better in
Jupyter Notebook.
Other eg
Quandl and
ALPHA VANTAGE.
Thanks snippsat,
I've run your code and got the below error.
Also, I'm using Spyder 3.7 and it asks to save the file, how do I then run it as a program?
Python 3.7.1 (default, Dec 10 2018, 22:09:34) [MSC v.1915 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 7.2.0 -- An enhanced Interactive Python.
runfile('D:/Users/xxxxxx/Desktop/untitled2.py', wdir='D:/Users/xxxxxx/Desktop')
Traceback (most recent call last):
File "<ipython-input-1-e5ce65afedb8>", line 1, in <module>
runfile('D:/Users/xxxxxx/Desktop/untitled2.py', wdir='D:/Users/xxxxxx/Desktop')
File "C:\Users\xxxxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\xxxxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/Users/xxxxxx/Desktop/untitled2.py", line 8, in <module>
import pandas_datareader.data as web
ModuleNotFoundError: No module named 'pandas_datareader'
Install with.
pip install pandas_datareader
Thanks snippsat,
A couple of other things have come up since and I'll need to change my access level to fix them.
I downloaded Anaconda so I saw one fix was to change your 'pip install pandas_datareader' to 'pip conda install pandas_datareader'.
This didn't work either so I tried on the cmd prompt.
The error was that 'pip' was not a recognised command.
So the fix is apparently changing the mapping of the install files.
I'll get this done & then I think your code will work.
Thanks for solving the problem!
Peter

(Jan-08-2019, 12:57 AM)OscarBoots Wrote: [ -> ]The error was that 'pip' was not a recognised command.
So the fix is apparently changing the mapping of the install files.
Yes is not added to Windows Path.
You can add it manually in
Environment Variables Path.
For you
C:\Users\xxxxxx\AppData\Local\Continuum\anaconda3\
and
C:\Users\xxxxxx\AppData\Local\Continuum\anaconda3\Scripts
Also the same as here,but with your path.
Quote:to 'pip conda install pandas_datareader'.
No Anaconda3 has both
conda
(Anaconda own installer) and
pip
(Python own installer).
So can use
conda install -c anaconda pandas-datareader
.
Example see that i have choice an easier Path than your looong
G:\Anaconda3
λ cd scripts
# Check pip
G:\Anaconda3\Scripts
λ pip -V
pip 18.1 from G:\Anaconda3\lib\site-packages\pip (python 3.7)
# Check conda
G:\Anaconda3\Scripts
λ conda -V
conda 4.5.12
# Install pandas-datareader with conda,pip also work
G:\Anaconda3\Scripts
λ conda install -c anaconda pandas-datareader
Solving environment: done
## Package Plan ##
environment location: G:\Anaconda3
added / updated specs:
- pandas-datareader
The following packages will be downloaded:
package | build
---------------------------|-----------------
openssl-1.1.1a | he774522_0 5.7 MB anaconda
pandas-datareader-0.7.0 | py37_0 141 KB anaconda
ca-certificates-2018.03.07 | 0 155 KB anaconda
------------------------------------------------------------
Total: 6.0 MB
The following NEW packages will be INSTALLED:
pandas-datareader: 0.7.0-py37_0 anaconda
The following packages will be UPDATED:
ca-certificates: 2018.03.07-0 --> 2018.03.07-0 anaconda
openssl: 1.1.1a-he774522_0 --> 1.1.1a-he774522_0 anaconda
Proceed ([y]/n)? y
Downloading and Extracting Packages
openssl-1.1.1a | 5.7 MB | ################################################################################################ | 100%
pandas-datareader-0. | 141 KB | ################################################################################################ | 100%
ca-certificates-2018 | 155 KB | ################################################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
# Test that it work
G:\Anaconda3\Scripts
λ cd ..
G:\Anaconda3
λ python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas_datareader.data as web
>>>
Look at
Anaconda and other ways to run Python