Python: object does not support item assignment - python

I did not understand the message sent by compiler. Can someone help me?
Traceback (most recent call last):
File "C:/Users/edboa_000/PycharmProjects/RESULTADOS_FINAIS 3/ggcc1/quadrante_aplicacao_forca9.py", line 13, in <module>
matriz1[i-11][j] = posicao[j]
TypeError: 'int' object does not support item assignment
My code:
matriz1=((30,2))
for i in range(11,41):
graof = np.genfromtxt('DMconf0%d.dat'%i,skip_header=10254, usecols=(0))
grao = graof.astype('int')
posicao = np.genfromtxt('DMconf0%d.dat'%i,skip_header=grao, usecols=(0,1), max_rows=1)
for j in range(2):
matriz1[i-11][j] = posicao[j] #line 13
print(matriz1)

Related

Syntax error with exec call to an object in Python 3

I really don't understand what causes the problem, could someone point it out for me please?
with shelve.open(obj_path) as obj:
for as_num in obj['as_number_list']: # ignore warning, obj['as_number_list'] is a list
temp = charge_as(obj['as_' + str(as_num)]) # temp is an object
as_test = temp # doing like this is ok
print(type(as_test))
exec("as_{}_obj = {}".format(as_num, temp)) # **error here**
And it gives syntax error like this:
<class 'instruments.AS'>
Traceback (most recent call last):
File "...", line 45, in <module>
exec("as_{}_obj = {}".format(as_num, temp))
File "<string>", line 1
as_1_obj = <instruments.AS object at 0x000002A86732E290>
^
SyntaxError: invalid syntax
I tried
exec("as_{}_obj = {}".format(as_num, temp.__dict__))
no error is shown but now as_{}_obj is of class 'dict' instead of class 'instruments.AS'
line 45:
exec("as_{}_obj = temp".format(as_num))

How to fix a AttributeError in Python?

I don't success to fix this error on my code. Python wrote me this error message:
"Traceback (most recent call last):
File "C:\Users\ChloƩ CHAUMETON\Documents\BioAxial\1_R&D\4_Software\2_Python\Measurement_script\Matlab_to_Python\2019\Chloe_script_V3.py", line 114, in <module>
if 0 < len((lrir)(lrir.name_file)):
AttributeError: 'function' object has no attribute 'name_file' "
I defined the "name_file = 'BSR_Main.exe2018_09_14.csv' " up of my code but I don't understand why python don't attribute this name fto the function. Here is my code.
i_r=[]
def lrir():
lrir = liste_record[i_r]
name_file = 'BSR_Main.exe2018_09_14.csv';
for i_r in range(0,rec-1):
if 0 < len((lrir)(lrir.name_file)):
str1 = ligne.find(lrir.name_file,'/')
pr_rep = os.path.join(dir_PR,s(i_r).name_file)
log_rep = os.path.join(pr_rep,'Log')
log_file = glob.glob(os.path.join(log_rep,'*.log'))
print('log file:',log_file)

Python: TypeError: 'str' object is not callable Rating System

When I run this code:
def printPredictions(matches):
pPredictionTable = PrettyTable()
pPredictionTable.field_names = ["Player 1", "Player 2", "Difference", "Winner"]
for match in matches:
p1 = match['teamA']
p2 = match['teamB']
if match['aBeatb'] == True:
pPredictionTable.add_row([match['teamA'], match['teamB'], match['difference'], p1])
else:
pPredictionTable.add_row([match['teamA'], match['teamB'], match['difference'], p2])
print(pPredictionTable)
printPredictions(pmatches)
I get this error:
Traceback (most recent call last):
File "C:\Users\ericr_000\Desktop\PyDev\NPA-2-Rating-System\Rankings.py", line 645, in <module>
printPredictions()
TypeError: 'str' object is not callable
I have pmatches as a separate dictionary, and I don't have the coding skills to fix this issue. (Line 145 is printPredictions(pmatches)
If you're getting 'str' object is not callable when you try to call printPredictions, that means that by the time your program reaches line 645, the name printPredictions was reassigned to a string. Somewhere in your code you have something like
printPredictions = someStringValueGoesHere
You should choose a different name for that variable, or delete the line entirely.
foobar = someStringValueGoesHere

python: TypeError: 'str' object is not callable

i'm trying to load a binary file with pickle that has a record in a list, like so
import pickle
class player_energy_usage():
def __init__(self):
self.weapons = 25
elf.shields = 25
self.life_support = 25
self.engines = 25
def p_eu_init():
global p_energy
p_energy = []
player_ship_energy = player_energy_usage()
p_energy.append(player_ship_energy)
pickle.dump(p_energy,open('p_energy.dat','wb'))
p_eu_init()
pickle.load('rb'('p_energy.dat'))
print('Weapons are using {0}% of energy'.format(p_energy[0].weapons))
print('Shields are using {0}% of energy'.format(p_energy[0].shields))
print('Life Support is using {0}% of energy'.format(p_energy[0].life_support))
print('Engines is using {0}% of energy'.format(p_energy[0].engines))
However i get a type error,
Traceback (most recent call last):
File "E:/Python texted based game/Tests/file loading test.py", line 18, in <module>
pickle.load('rb'('p_energy.dat'))
TypeError: 'str' object is not callable
thanks for the help.
That is not the correct syntax. It should be instead:
p_energy = pickle.load(open('p_energy.dat', 'rb'))
What you're actually doing is:
'rb'('p_energy.dat') is trying to call the str object 'rb' with an argument of 'p_energy.dat', which is why you get the error 'str' object is not callable.

"TypeError: 'unicode' object does not support item assignment" in dictionaries

I am trying to build/update a dictionary. I have nicknames as keys in temp_dict and looking for ids to add.
Excerpt form my code. I think it is enough for you to see my mistake.
d1 = {u'status': u'ok', u'count': 1, u'data': [{u'nickname': u'45sss', u'account_id': 553472}]}
temp_dict = {}
for key, value in d1.iteritems():
if "data" == key:
for dic2 in value:
x = dic2['nickname']
y = dic2['account_id']
temp_dict[x] = y;
My error:
Traceback (most recent call last):
File "untitled.py", line 36, in <module>
get_PlayerIds_Names_WowpApi_TJ_() #Easy going. Some issues with case letters.
File "g:\Desktop\Programming\WOWP API\functions.py", line 44, in get_PlayerIds_Names_WowpApi_TJ_
check_missing_player_ids(basket)
File "g:\Desktop\Programming\WOWP API\functions.py", line 195, in check_missing_player_ids
temp_dict[x] = y;
TypeError: 'unicode' object does not support item assignment
There are multiple SO entries regarding the same error. But no are connected to such dictionary manipulation.
Most likely you have put unicode string in temp_dict somewhere:
>>> temp_dict = u''
>>> dic2 = {u'nickname': u'45sss', u'account_id': 553472}
>>> x = dic2['nickname']
>>> y = dic2['account_id']
>>> temp_dict[x] = y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'unicode' object does not support item assignment
init it with empty dict and all will work:
>>> temp_dict = {}
>>> temp_dict[x] = y
>>> temp_dict
{u'45sss': 553472}

Categories

Resources