How to fix string concatenation error in Python 3 - python-3.x

When I run the code below, it begins to loop through and returns the time and length of temp_data, but before reaching 100 in the loop throws the error. If I update the code to sleep for 5 seconds instead of 1, it will make it through all 100 iterations.
start_date = 1483228800*1000 #jan 1 2017
pair = 'ETHBTC'
timeframe = '1m'
final_data = []
for _ in range(0,100):
url = 'https://api.bitfinex.com/v2/candles/trade:'+timeframe+':t'+pair+'/hist?sort=1&limit=1000&start='+str(start_date)
r = requests.get(url)
temp_data = r.json()
final_data = final_data+temp_data
start_date = temp_data[len(temp_data)-1][0]+60*1000
print(time.ctime(), len(temp_data))
time.sleep(1)
print(len(final_data))
Error:
Traceback (most recent call last):
File
"/Users/michael/PycharmProjects/bot/venv/datasets/dataset.py", line
18, in <module>
start_date = temp_data[len(temp_data)-1][0]+60*1000
TypeError: can only concatenate str (not "int") to str

you should convert 60*1000 to str.
start_date = temp_data[len(temp_data)-1][0] + str(60*1000)

Related

TheGuardian API - Script crashes

import json
import requests
from os import makedirs
from os.path import join, exists
from datetime import date, timedelta
ARTICLES_DIR = join('tempdata', 'articles')
makedirs(ARTICLES_DIR, exist_ok=True)
API_ENDPOINT = 'http://content.guardianapis.com/search'
my_params = {
'q': 'coronavirus,stock,covid',
'sectionID': 'business',
'from-date': "2019-01-01",
'to-date': "2020-09-30",
'order-by': "newest",
'show-fields': 'all',
'page-size': 300,
'api-key': '### my cryptic key ###'
}
# day iteration from here:
# http://stackoverflow.com/questions/7274267/print-all-day-dates-between-two-dates
start_date = date(2019, 1, 1)
end_date = date(2020,9, 30)
dayrange = range((end_date - start_date).days + 1)
for daycount in dayrange:
dt = start_date + timedelta(days=daycount)
datestr = dt.strftime('%Y-%m-%d')
fname = join(ARTICLES_DIR, datestr + '.json')
if not exists(fname):
# then let's download it
print("Downloading", datestr)
all_results = []
my_params['from-date'] = datestr
my_params['to-date'] = datestr
current_page = 1
total_pages = 1
while current_page <= total_pages:
print("...page", current_page)
my_params['page'] = current_page
resp = requests.get(API_ENDPOINT, my_params)
data = resp.json()
all_results.extend(data['response']['results'])
# if there is more than one page
current_page += 1
total_pages = data['response']['pages']
with open(fname, 'w') as f:
print("Writing to", fname)
# re-serialize it for pretty indentation
f.write(json.dumps(all_results, indent=2))
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-18-f04b4f0fe9ed> in <module>
49 resp = requests.get(API_ENDPOINT, my_params)
50 data = resp.json()
---> 51 all_results.extend(data['response']['results'])
52 # if there is more than one page
53 current_page += 1
KeyError: 'results'
Same error occurs for 'pages'
At first there was no issues and was able to run it. Download crashed after 2020-03-24. Since then can't get the code running again.
I'm referring to Line 51 and 54. At least at this point the codes crashes.
Not sure how to get rid of the issue. Any ideas?
Understanding the error message would be the first step - it compains about a missing key. Check if data['response']['results'] is present (hint: it is not) and check what exactly the structure of your data['response'] is.
Fortunately one can use the api parameter 'test' so we can help using that key:
my_params = {
'q': 'coronavirus,stock,covid',
'sectionID': 'business',
'from-date': "2019-01-01",
'to-date': "2020-09-30",
'order-by': "newest",
'show-fields': 'all',
'page-size': 300,
'api-key': 'test' # test key for that API
}
On running, I get the same exception, inspect data['response'] and get:
Lets see what parameters are given, shall we?
my_params = {
'q': 'coronavirus,stock,covid',
'sectionID': 'business',
'from-date': "2019-01-01",
'to-date': "2020-09-30",
'order-by': "newest",
'show-fields': 'all',
'page-size': 300, # TOO BIG
'api-key': 'test'
}
Fix that to 200 and you'll get
Downloading 2019-01-01
...page 1
Writing to tempdata\articles\2019-01-01.json
Downloading 2019-01-02
...page 1
Writing to tempdata\articles\2019-01-02.json
Downloading 2019-01-03
...page 1
Writing to tempdata\articles\2019-01-03.json
Downloading 2019-01-04
...page 1
Writing to tempdata\articles\2019-01-04.json
Downloading 2019-01-05
[snipp]

TypeError: 'NoneType' object is not iterable (Python3 with Oracle 19c)

Python 3.6.3 /
Oracle 19c
The following script runs fine till it hits upc_id, = cur.fetchone(). Could someone explain, please what may cause it? If I run the query in database, I get the result back (see below). Is there a way to see exactly what Oracle runs, after variable substitution? I suspect single quotes are not in place for bind variables, but how can I confirm?
import datetime
import cx_Oracle
import db
line_item_sku = 'Y35FLPQ_034Y_M'
x = line_item_sku.split("_")
print (x)
print ("Split-list len: "+ str(len(x)))
if len(x) == 3:
sku_with_dimension = False
elif len(x) == 4:
sku_with_dimension = True
print ("SKU with dimension: " + str(sku_with_dimension))
style_id = x[0]
color_id = x[1]
size_id = x[2]
if sku_with_dimension:
dimension_id = x[3]
print ("Style: "+style_id)
print ("Color: "+color_id)
print ("Size: "+size_id)
conn = db.connect('Galo')
print ("Connected to: " + conn.version)
cur = conn.cursor()
upc_id = cur.var(str)
print ("Assigned return value")
if sku_with_dimension:
sql = ("""
select upc_id
from sku
where business_unit_id = '81'
and style_id = :1
and color_id = :2
and identifier_id = 'EA'
and size_id = :3
and dimension_id = :4
""")
cur.execute(sql,(style_id, color_id, size_id, dimension_id))
else:
sql = ("""
select upc_id
from sku
where business_unit_id = '81'
and style_id = :1
and color_id = :2
and identifier_id = 'EA'
and size_id = :3
""")
cur.execute(sql,(style_id, color_id, size_id))
print ("Determined which query to run")
upc_id, = cur.fetchone()
print (upc_id)
db.disconnect(conn, cur)
Here is the output
'Y35FLPQ', '034Y', 'M']
Split-list len: 3
SKU with dimension: False
Style: Y35FLPQ
Color: 034Y
Size: M
Connected to: 19.0.0.0.0
Assigned return value
Determined which query to run
Traceback (most recent call last):
File "c:/Python/code/test.py", line 66, in <module>
upc_id, = cur.fetchone()
TypeError: 'NoneType' object is not iterable
If I run the query in database, I receive a result back:

Unable to a convert given date to a required date format that is coming from the df['dateformat'] In python

MeetingDate = datetime.datetime(2020,5,11)
df = pd.read_csv('request.csv')
dateformatsXpaths = df['dateformat']
MeetingDateXpaths = df['WebCfg_MtgDate_xpath']
urls = df['url']
fcids = df['fcid']
for url in urls:
driver.get(url)
agendaMeetingdateformat = MeetingDate.strftime(df['dateformat'])
meeting =df['WebCfg_MtgDate_xpath'].replace('*', agendaMeetingdateformat)
my_element = driver.find_element_by_xpath(meeting).text
if my_element == agendaMeetingdateformat :
print('valid Meeting date')
else:
print('Invalid Meeting date')
driver.close()
I want to convert the given date to the date format that is coming from the df['dateformat'],but its throwing following error
Traceback (most recent call last):
File "C:\Users\sameer\PycharmProjects\Python\MeetingDateFinder.py", line 32, in <module>
agendaMeetingdateformat = MeetingDate.strftime(df['datefromat'])
TypeError: strftime() argument 1 must be str, not Series

Python3.6 - TypeError: 'int' object is not subscriptable

Trying to do the addition of given integer such as 123435 = (1+2+3 = 6) and (4+3+5 = 12) and checking they are equal or not. I am facing an error in following code. I am using python 3.6
def isEqual(n):
num = int(n)
val = len(str(n))
mid = len(str(val))//2
return sum(num(val[:mid])) == sum(num(val[mid:]))
print(isEqual(132435))
val = len(str(n)) ( is is of type int ) you can't make int[:] only with string it works
def isEqual(n):
val = len(str(n))//2
first_sum = eval(('+').join(str(n)[:val]))
second_sum = eval(('+').join(str(n)[val:]))
return first_sum == second_sum
print(isEqual(132435))
try this
return sum(num(val[:mid])) == sum(num(val[mid:]))
Here val value is 6, It is integer you cant use slicing on integer. sum(num(val[:mid]))
You are trying to slice integer, which results in error.
>>> 10[:5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
>>>

Python web app getting valueerror 0 not in list

#app.route('/')
def index():
tpopDloads = popDloads
tpopShipped = popShipped
locPopDload = []
locPopShipped = []
popDinfo = []
popSinfo = []
popDloadsOrd = sorted(tpopDloads, reverse=True)
popShippedOrd = sorted(tpopShipped, reverse=True)
for i in range(3):
locPopDload.append(tpopDloads.index(popDloadsOrd[i]))
popDinfo.append(dProducts[locPopDload[i]])
tpopDloads[tpopDloads.index(popDloadsOrd[i])] = -1 #Problem line#
for i in range(3):
locPopShipped.append(tpopShipped.index(popShippedOrd[i]))
popSinfo.append(sProducts[locPopShipped[i]])
tpopShipped[tpopShipped.index(popDloadsOrd[i])] = -1
return render_template('index.html', popDinfo=popDinfo, popSinfo=popSinfo)
The error I'm getting is:
File "/var/lib/openshift/5697165a0c1e66eb870000fb/app-root/runtime /repo/flaskapp.py", line 47, in index
tpopShipped[tpopShipped.index(popDloadsOrd[i])] = -1
ValueError: 0 is not in list
This is using two variable that are popDloads and popShipped which are both lists that contain a set of integers. I don't see why it's not working as it is trying to find the actual index of a number after the list has been ordered. It also works when the numbers are all zero, it's only after I change the numbers in another piece of code that I run into problems.
The error is telling you why your code isn't working. 0 isn't in the list tpopShipped.
>>> [1,2,3].index(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 0 is not in list

Resources