Error indexError: list index out of range - python

My scirpt :
if 'recheck' == msg.lower():
with open('Output.txt','r') as rr:
contactArr = rr.readlines()
for v in xrange(len(contactArr) -1,0,-1):
num = re.sub(r'\n', "", contactArr[v])
contacts.append(num)
pass
contacts = list(set(contacts))
for z in range(len(contacts)):
arg = contacts[z].split('|')
if arg[1] == receiver.id :
userList.append(arg[0])
timelist.append(arg[2])
uL = list(set(userList))
# print uL
for ll in range(len(uL)):
try:
getIndexUser = userList.index(uL[ll])
timeSeen.append(strftime("%H:%M:%S", localtime(int(timelist[getIndexUser]) / 1000)))
recheckData.append(userList[getIndexUser])
except IndexError:
conName.append('nones')
pass
contactId = client._getContacts(recheckData)
for v in range(len(recheckData)):
dataResult.append(contactId[v].displayName + '['+timeSeen[v]+']')
pass
# # print len(recheckData)
tukang = "V=ON Members=V\n[*]"
grp = '\n[*] '.join(str(f) for f in dataResult)
receiver.sendMessage("%s %s" % (tukang, grp))
But error in terminal :
Traceback (most recent call last):
File "echobot.py", line 117, in <module>
if arg[1] == receiver.id :
IndexError: list index out of range
Can you help me?

The error is coming from
arg = contacts[z].split('|')
if arg[1] == receiver.id :
userList.append(arg[0])
timelist.append(arg[2])
you should double check that your contacts are all formatted correctly.
According to this code, each contact should be formatted like
user|id|time
If each contact in contacts is not formatted exactly that way, this error will be thrown. It looks like your split('|') fucntion isn't finding any '|' to split on.

Related

unresolved attribute reference 'replace' for class 'int' on the replace function

I don't understand why the replace got unresolved attribute reference 'replace' for class 'int' error. And then when the money reach more than 1000, I got value error, even though I tried to handle it with exception by removing comma from the string :
Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\Day48_selenium_cookie_clicker\main.py", line 37, in <module>
money = int(cookies_owned.text)
ValueError: invalid literal for int() with base 10: '1,012'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\Day48_selenium_cookie_clicker\main.py", line 50, in <module>
money = int(cookies_owned.text)
ValueError: invalid literal for int() with base 10: '1,012'
This is my code:
from selenium import webdriver
import time
from timeit import default_timer as timer
chrome_driver_path = "C:\Development\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get("http://orteil.dashnet.org/experiments/cookie/")
timeout = 30 # [seconds]
time_check = time.time() + 5
ids = driver.find_elements_by_xpath('//*[#id]') # find all ids within a webpage
ids_list = [i.get_attribute('id') for i in ids]
items_for_sale_ids = [i for i in ids_list if i[0] == "b" and i[1] == "u" and i[2] == "y"][:8]
items_data = [i.text.split("\n") for i in driver.find_elements_by_id("store")][0]
items_for_sale = [items_data[i].split(" - ") for i in range(len(items_data)) if i % 2 == 0]
price = [int(y[1].replace(",", "")) for y in items_for_sale]
items_pricelist = list(zip(items_for_sale_ids, price))
items_pricelist_dict = {i[1]: i[0] for i in items_pricelist}
start = timer()
cookie = driver.find_element_by_id("cookie")
while True:
cookie.click()
if time.time() > time_check:
# print("hello")
cookies_owned = driver.find_element_by_id("money")
try:
money = int(cookies_owned.text)
affordable_upgrades = {}
for cost, id in items_pricelist_dict.items():
if money > cost:
affordable_upgrades[cost] = id
max_upgrade = max(affordable_upgrades)
print(max_upgrade)
to_purchase_id = affordable_upgrades[max_upgrade]
to_buy = driver.find_element_by_id((to_purchase_id))
to_buy.click()
time_check = time.time() + 5
except ValueError:
money = int(cookies_owned.text)
formatted_money = money.replace(",", "")
affordable_upgrades = {}
for cost, id in items_pricelist_dict.items():
if formatted_money > cost:
affordable_upgrades[cost] = id
max_upgrade = max(affordable_upgrades)
print(max_upgrade)
to_purchase_id = affordable_upgrades[max_upgrade]
to_buy = driver.find_element_by_id((to_purchase_id))
to_buy.click()
time_check = time.time() + 5
see below.
(the problem is the , in value. once we remove it - it works)
value = '1,012'
try:
int_value = int(value)
except ValueError:
print('failed - lets try again..')
int_value = int(value.replace(',',''))
print(int_value)
output
failed - lets try again..
1012
I got value error, even though I tried to handle it with exception by removing comma from the string :
try:
money = int(cookies_owned.text)
...
except ValueError:
money = int(cookies_owned.text)
...
Yes, you handle Exception and after that raise same error again ;)
Correct code is:
try:
money = int(cookies_owned.text)
...
except ValueError:
money = int(cookies_owned.text.replace(",", ""))
...
Or simpler:
try:
money = int(cookies_owned.text.replace(",", ""))
...
except ...
Here is no problem to replace , anytime. You don't need to wait for exception. replace(",", "") will be correct for any number (1, 100, 1,000, 1,000,000, ...)
This won't work: (see comments inserted in the code)
money = int(cookies_owned.text) # money is now an int
formatted_money = money.replace(",", "") # ... but you treat it as a string

TypeError: '<' not supported between instances of 'str' and 'float' - Prettytable Error

I'm getting the above error trying to run my Py script. It also throws error in the prettytable code which is weird, as well, since I use it fine all the time. This is basically a web-scraping script that I made for use with an airline manager game I play.
Code:
if "top" in str(r): # log-in was successful.
if True: # just to make it collapsible
def getHTML(url):
return http.request('GET', url, headers={'Cookie': 'PHPSESSID='+phpSid}).data
def findBetween1(s, start, end):
try:
return str((s.split(start))[1].split(end)[0])
except:
return ""
def getHTMLthreaded(infoArray): # infoArray: [url, detail, detail]
def getHTML(url):
return http.request('GET', url, headers={'Cookie': 'PHPSESSID='+phpSid}).data
with ThreadPoolExecutor(max_workers=threads) as executor:
futureToURL = {executor.submit(getHTML, info): info for info in infoArray}
responseResults = []
for future in as_completed(futureToURL):
responseResults.append(future.result()) # [data, url]
return responseResults
def median(lst):
lst = [int(i) for i in lst]
n = len(lst)
s = sorted(lst)
return int((sum(s[n//2-1:n//2+1])/2.0, s[n//2])[n % 2]) if n else None
acURLs = []
fleet = BeautifulSoup(getHTML('https://www.airline4.net/fleet.php?undefined'), "lxml").find_all(onclick=re.compile('Ajax')) # get a list of fleet
for acTypeId in fleet:
acURLs.append('https://www.airline4.net/fleet.php?type='+findBetween1(str(acTypeId), 'type=', "'")) # for each fleet type, get a list of aircraft belonging to that type
acMenus = getHTMLthreaded(acURLs)
# t = PrettyTable([])
acIds = []
for acMenu in acMenus:
acMenuItems = BeautifulSoup(acMenu, "lxml").find_all(onclick=re.compile('routes_main.php'))
for thisAcMenu in acMenuItems:
acIds.append('https://www.airline4.net/fleet_details.php?id='+findBetween1(str(thisAcMenu['onclick']), 'id=', "','"))
# acIds = ['https://www.airline4.net/fleet_details.php?id=1323552']
print('Sorting by "profit/pax", indicating how well the route actually profits.')
t = PrettyTable(['model', 'reg', 'chkH', 'wear', 'config', 'fn', 'route', u'$\u02dc', u'CO\u2082\u02dc', 'fuel', 'prof/pax', 'prof/hrs'])
acDetails = getHTMLthreaded(acIds)
for acDet in acDetails:
acDet = BeautifulSoup(acDet, "lxml")
mainTable = acDet.find(class_='col-sm-6 bg-light border').find_all(class_='m-text')
seatLayout = acDet.find_all(class_='col-4 p-2 text-center exo')
acType = str(mainTable[0].string)
fleetReg = str(acDet.find(id='ff-name').string)
chk = str(mainTable[2].string) # hours to check
wear = str(mainTable[6].string) # wear percentage
yS = str(seatLayout[0].find('br').next_sibling.strip().rjust(3))
jS = str(seatLayout[1].find('br').next_sibling.strip().rjust(3))
fS = str(seatLayout[2].find('br').next_sibling.strip().rjust(3))
config = yS + "/" + jS + "/" + fS
try: # in case the aircraft is not routed:
routeReg = str(acDet.find(id='rr-name').string)
stop = str(acDet.find(class_='xs-text pt-2')).replace('<div class="xs-text pt-2">Via<br/> ','').replace('</div>','')
stop = "" if stop == "None" else "·" + stop
route = str(acDet.find_all(class_='l-text exo')[0].string) + stop + "·" + str(acDet.find_all(class_='l-text exo')[1].string)
inc, co2 = [], []
for thisFlight in acDet.find_all(class_='row bg-light m-text p-1 border'):
inc.append(str(thisFlight.find(class_='col-3 text-right text-success').find('b').string).replace('$', '').replace(',', ''))
co2.append(str(thisFlight.find(class_='s-text').string).replace(' Quotas', '').replace(',', ''))
prices = thisFlight.find_all(class_='col-3')[2]
inc, co2 = median(inc), median(co2)
fuel = str(acDet.find(class_='row bg-light m-text p-1 border').find_all(class_='s-text')[1].string).replace(' Lbs', '').replace(',', '')
profit = inc - (co2 * co2Price / 1000) - (int(fuel) * fuelPrice / 1000)
depTime = acDet.find_all(class_='col-6 bg-white border s-text')[0].string.strip().replace(' UTC', '').split(':')
arrTime = acDet.find_all(class_='col-6 bg-white border s-text')[1].string.strip().replace(' UTC', '').split(':')
depSec = int(depTime[0])*3600 + int(depTime[1])*60 + int(depTime[2])
arrSec = int(arrTime[0])*3600 + int(arrTime[1])*60 + int(arrTime[2])
hrsElapsed = ((86400 - (depSec - arrSec)) if depSec > arrSec else arrSec - depSec) / 3600
profPax = round(profit / (int(yS) + int(jS)*2 + int(fS)*3),3)
profHrs = round(profit / hrsElapsed, 3)
t.add_row([acType, fleetReg, chk, wear, config, routeReg, route, inc, co2, fuel, profPax, profHrs])
except: # only show the fleet details, but no other metrics
# print(sys.exc_info()[0])
t.add_row([acType, fleetReg, chk, wear, config, "", "", "", "", "", "", ""])
t.sortby = 'prof/pax'
print(t)
else:
print('Your e-mail and/or password was wrong.')
Traceback:
Traceback (most recent call last):
File "dX.py", line 123, in <module>
print(t)
File "C:\Users\SSM\AppData\Local\Programs\Python\Python38\lib\site-packages\prettytable.py", line 237, in __str__
return self.__unicode__()
File "C:\Users\SSM\AppData\Local\Programs\Python\Python38\lib\site-packages\prettytable.py", line 243, in __unicode__
return self.get_string()
File "C:\Users\SSM\AppData\Local\Programs\Python\Python38\lib\site-packages\prettytable.py", line 984, in get_string
rows = self._get_rows(options)
File "C:\Users\SSM\AppData\Local\Programs\Python\Python38\lib\site-packages\prettytable.py", line 933, in _get_rows
rows.sort(reverse=options["reversesort"], key=options["sort_key"])
TypeError: '<' not supported between instances of 'str' and 'float'
When you are handling an exception, you add a default value that can't be compared with your floating-point numbers:
except:
t.add_row([acType, fleetReg, chk, wear, config, "", "", "", "", "", "", ""])
# these are all strings ^^ ^^ ^^ ^^ ^^ ^^ ^^
If you want to sort on the prof/pax column, you'd have to give that column only numeric values. Perhaps 0 would suffice for you?
except:
t.add_row([acType, fleetReg, chk, wear, config, "", "", "", "", "", 0, ""])
# numeric value for profPax ^^
Alternatively, you can specify a sort key, called to transform each value in the sorted column to something to compare by. You could use that to replace strings with zeros only when sorting:
t.sort_key = lambda value: value or 0
This makes use of the fact that both "" and 0 are false-y and so or will pick the second value to return, 0:
>>> value = ""
>>> value or 0
0
Side note: it is rarely a good idea to use except: without qualification. If your code has, say, zero-division issues, only catch that exception:
except ZeroDivisionError:
t.add_row([acType, fleetReg, chk, wear, config, "", "", "", "", "", "", ""])
You can always add more exceptions; your code could potentially raise ValueErrors (from int() conversions and the like) and AttributeError (from trying to treat None values as if they are BeautifulSoup elements, for example), so you could catch only those 3:
except (ZeroDivisionError, ValueError, AttributeError):
t.add_row([acType, fleetReg, chk, wear, config, "", "", "", "", "", "", ""])
That makes it a lot easier to detect bugs in your code later on, and would let you more easily interrupt your program if it is taking to long as you are then not catching the KeyboardInterrupt exception.

Error 'numpy.int32' object does not support item assignment

I get this error
Traceback (most recent call last):
File "C:\Users\User1\Desktop\cellh5_scripts\ewa_pnas_fate.py", line 90, in <module>
ec.combine_classifiers("Event labels combined")
File "C:\Users\User1\Desktop\cellh5_scripts\ewa_pnas_fate.py", line 53, in combine_classifiers
pnas_class[pnas_class==3] = 1
TypeError: 'numpy.int32' object does not support item assignment
by runing the code
def combine_classifiers(self, output_name):
all_combined_classes = []
for _, (plate_name, w, p, t1, t2, track_ids, track_labels) in self.mapping[['Plate',
'Well',
'Site',
'Gene Symbol',
'siRNA ID',
'Event track ids',
'Event track labels']].iterrows():
combined_classes = []
ch5_file_handle = self.cellh5_handles[plate_name]
ch5_pos = ch5_file_handle.get_position(w, str(p))
for track_id, track_label in zip(track_ids, track_labels):
h2b_class = track_label.copy()
print(track_id)
pnas_class = ch5_pos.get_class_prediction('secondary__expanded')[track_id]['label_idx'] + 1
print(pnas_class)
inter_idx = h2b_class == 1
pnas_class[pnas_class==3] = 1
pnas_class[pnas_class==2]+=2
combined_class = h2b_class
combined_class[inter_idx] = pnas_class[inter_idx]
combined_classes.append(combined_class)
all_combined_classes.append(combined_classes)
self.mapping[output_name] = pandas.Series(all_combined_classes)
I print pnas_class which is 1, and track_id which is 50708. I'm wondering what the designer of code want to do in the part:
inter_idx = h2b_class == 1
pnas_class[pnas_class==3] = 1
pnas_class[pnas_class==2]+=2
combined_class = h2b_class
combined_class[inter_idx] = pnas_class[inter_idx]
How can I change that to have the same meaning?
pnas_class is a an integer so you can't select item from an integer by [pnas_class==3] = 1.
Maybe you are trying to affect 1 to pnas_class if it's equal to 3. In this case try this:
pnas_class= 1*(pnas_class == 3) + pnas_class*(pnas_class != 3 )
Ok I found the mistake. You arer right the pnas_class should not be an integer and I know why is it integer instead of array.

Tuple indices must be integers when building a dictionary

I am taking an Udacity programming course and have been sitting on the same problem for a week. I finally think I am close to getting it right, but I don't get the last objection. Here is my code:
def process_file(f):
# This is example of the datastructure you should return
# Each item in the list should be a dictionary containing all the relevant data
# Note - year, month, and the flight data should be integers
# You should skip the rows that contain the TOTAL data for a year
# data = [{"courier": "FL",
# "airport": "ATL",
# "year": 2012,
# "month": 12,
# "flights": {"domestic": 100,
# "international": 100}
# },
# {"courier": "..."}
# ]
data = []
info = {}
info["courier"], info["airport"] = f[:6].split("-")
with open("{}/{}".format(datadir, f), "r") as html:
soup = BeautifulSoup(html)
car = str(html)[17:19]
airp = str(html)[20:23]
mydict = {}
x = 0
table = soup.find("table", {"class": "dataTDRight"})
rows = table.find_all('tr')
for row in rows:
cells = row.find_all('td')
year = cells[0].get_text()
year = (year.encode('ascii'))
Month = cells[1].get_text()
Month = (Month.encode('ascii'))
domestic = cells[2].get_text()
domestic = (domestic.encode('ascii'))
international = cells[3].get_text()
international = (international.encode('ascii'))
if Month != "Month" and Month != "TOTAL":
Month = int(Month)
year = int(year)
domestic = int(domestic.replace(',', ''))
international = int(international.replace(',', ''))
mydict['courier'] = car
mydict['airport'] = airp
mydict['year'] = year
mydict['month'] = Month
mydict['flights'] = (domestic, international)
data.append(mydict.copy())
#print type(domestic)
#print mydict
print data
return data
def test():
print "Running a simple test..."
open_zip(datadir)
files = process_all(datadir)
data = []
for f in files:
data += process_file(f)
assert len(data) == 399
for entry in data[:3]:
assert type(entry["year"]) == int
assert type(entry["month"]) == int
assert type(entry["flights"]["domestic"]) == int
assert len(entry["airport"]) == 3
assert len(entry["courier"]) == 2
assert data[-1]["airport"] == "ATL"
assert data[-1]["flights"] == {'international': 108289, 'domestic': 701425}
print "... success!"
The error message I get is:
Traceback (most recent call last):
File "vm_main.py", line 33, in <module>
import main
File "/tmp/vmuser_elbzlfkcpw/main.py", line 2, in <module>
import studentMain
File "/tmp/vmuser_elbzlfkcpw/studentMain.py", line 2, in <module>
process.test()
File "/tmp/vmuser_elbzlfkcpw/process.py", line 114, in test
assert type(entry["flights"]["domestic"]) == int
TypeError: tuple indices must be integers, not str
I am a total beginner, I checked both the type of domestic, and international, they are both int.
Can anybody tell me where I can look up or what I did wrong?
You created a tuple here:
mydict['flights'] = (domestic, international)
so mydict['flights'] is a tuple. But you try to treat it as a dictionary here:
assert type(entry["flights"]["domestic"]) == int
That won't work; you'll need to use integer indices here:
assert type(entry["flights"][0]) == int
or better still, use isinstance() to test for types:
assert isinstance(entry["flights"][0], int)
Here you assign your data mydict['flights'] as a tuple.
def process_file(f):
# Omitted code...
mydict['flights'] = (domestic, international)
Your error then comes from an illegal access to that data type. You are attempting to access the first item of that tuple by the name of variable you used in assignment:
assert type(entry["flights"]["domestic"]) == int
You either need to access your data via an integer index:
assert type(entry["flights"][0]) == int
Or you need to change your assignment to:
mydict['flights'] = {"domestic":domestic, "international":international}
tuples are immutable data types which are indexed by integers. The type of access you are attempting is typical of a dictionary, where indexes can be of any type.

Handling and bypassing "TypeError: 'NoneType' " in python code

So I have this code which prints out the minimum cost and restaurant id for the item/items. The customer doesnt want to visit multiple restaurants. So for example if he asks for "A,B" then the code should print shop which offers them both , instead of scattering the user requirement around different restaurants (even if some restaurant is offering it cheap).
The error is basically coming because both the item ('burger' and 'D') are not available "together" at any of the two restaurants (1 & 2). But instead of throwing such long error , I'd simply like to print "One of item not available at a resto" etc.
Other such error throwing cominations are solver(shop_text,['tofulog', 'D']) , because 'tofulog' is only available at restaurant_1, wheras 'D' is available only at restaurant_2.
def build_shops(shop_text):
shops = {}
for item_info in shop_text:
shop_id,cost,items = item_info.replace('\n', '').split(',')
cost = float(cost)
items = items.split('+')
if shop_id not in shops:
shops[shop_id] = {}
shop_dict = shops[shop_id]
for item in items:
if item not in shop_dict:
shop_dict[item] = []
shop_dict[item].append([cost,items])
return shops
def solve_one_shop(shop, items):
if len(items) == 0:
return [0.0, []]
all_possible = []
first_item = items[0]
if first_item in shop:
for (price,combo) in shop[first_item]:
#print "items,combo=",items,combo
sub_set = [x for x in items if x not in combo]
#print "sub_set=",sub_set
price_sub_set,solution = solve_one_shop(shop, sub_set)
solution.append([price,combo])
all_possible.append([price+price_sub_set, solution])
if all_possible:
cheapest = min(all_possible, key=(lambda x: x[0]))
return cheapest
def solver(input_data, required_items):
shops = build_shops(input_data)
result_all_shops = []
for shop_id,shop_info in shops.iteritems():
this_shop = solve_one_shop(shop_info, required_items)
if this_shop is not None:
(price, solution) = this_shop
result_all_shops.append([shop_id, price, solution])
shop_id,total_price,solution = min(result_all_shops, key=(lambda x: x[1]))
print('SHOP_ID=%s' % shop_id)
sln_str = [','.join(items)+'(%0.2f)'%price for (price,items) in solution]
sln_str = '+'.join(sln_str)
print(sln_str + ' = %0.2f' % total_price)
shop_text = open('input-1.csv','rb')
solver(shop_text,['burger', 'D'])
=====input-1.csv=====restaurant_id, price, item
1,2.00,burger
1,1.25,tofulog
1,2.00,tofulog
1,1.00,chef_salad
1,1.00,A+B
1,1.50,A+CCC
1,2.50,A
2,3.00,A
2,1.00,B
2,1.20,CCC
2,1.25,D
======OUTPUT=======
Traceback (most recent call last):
File "26mar_cheap.py", line 106, in <module>
final_out(restaurant_read,sys.argv[2:])
File "26mar_cheap.py", line 92, in final_out
this_resto = requirement_one_restaurant(shop_info, required_items)
File "26mar_cheap.py", line 77, in requirement_one_restaurant
cost_sub_set,solution = requirement_one_restaurant(shop, sub_set)
TypeError: 'NoneType' object is not iterable
The traceback doesn't seem to be relevant to the code you've posted: the error is in requirement_one_restaurant but you've only posted solve_one_shop.
However, assuming the pattern of the code is similar, you're apparently doing some sort of recursive call. When you do this you need to ensure that all possible paths return a value. In solve_one_shop, for example, if all_possible is still empty by the end of the function then nothing will be returned, which will lead to the NoneType error.

Categories