Python TypeError: argument to string formatting

211 views Asked by At

I have been stuck on this problem for days now trying to figure out what the problem is. I keep getting below error:

TypeError: not all arguments converted during string formatting

Here is my code:

import numpy as np
import pandas as pd
import datetime as dt 
from urllib import urlretrieve
%matplotlib inline

url3='http://hopey.netfonds.no/tradedump.php?date= 20150523&paper=AAPL.O&csv_format=csv'

year = '2015' 
month = '05'
days = ['20', '21', '22', '23', '24']

AAPL = pd.DataFrame()
for day in days:
    AAPL = AAPL.append(pd.read_csv(url3 % (year, month, days), index_col=0, header=0, parse_dates=True))

AAPL.columns = ['bid', 'bdepth', 'bdeptht', 'offer', 'odepth', 'odeptht']
AAPL.info()
1

There are 1 answers

5
Martijn Pieters On

You don't have any placeholders in url3 and Python thus tells you it cannot insert the year, month or day values into it.

You need to use %[formattercode] strings to tell Python where to interpolate the values; %s will take strings for insertion (and you already have strings):

url3 = 'http://hopey.netfonds.no/tradedump.php?date=%s%s%s&paper=AAPL.O&csv_format=csv'
#                                                   ^^^^^^

Those 3 %s sequences tell Python to expect 3 values to be inserted there.

You also try to interpolate the whole days list. Use your loop variable instead:

url3 % (year, month, day)