D
D
Dmitry Shevchenko2020-08-20 13:23:01
Python
Dmitry Shevchenko, 2020-08-20 13:23:01

Problem with writing value to dictionary inside class?

I have a dictionary and a class inside which I write a value to it -
dict1 [dict1[self.id]] = self. val
but when I try to read the dictionary, I see that nothing has been written, the variables are the shaft and ID of the string type, but I suspect that the matter is in the self itself, I don’t know much about it, I registered the global, but nothing has changed. no errors either.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Shevchenko, 2020-08-20
@ZER0x32

class dataBase :
    def __init__ (self,base_name):
        self.base_name=str(base_name)
    def read (self):
        base = open(str(self.base_name), 'r')
        base_r = base.readlines()
        print ('!!!!!!!!!!!!!!!!!!!!!!!!!!!!',base_r)
        st1 = ''
        dict_r = ''
        dict_r = {}
        tx=0
        while tx <len(base_r)  :

           print ('tx :',tx,'of ',len(base_r))
           base_re = base_r[tx]
           st1 = base_re.split (':')
           print(st1[0],st1[1])
           dict_r [st1[0]] = st1[1][0]
           tx+=1

        print('resulr READING - ', dict_r)
        global stick
        if str(self.base_name) == "dataSti.txt" :
            print ('reading ',self.base_name,' end!')
            stick = dict_r
        base.close()
        return dict_r
    def write (self,id,val):
        self.id = id
        self.val=val
        print('write id - ',self.id)
        self.val = self.val
        print('write val - ', self.val, type(self.val))
        dat1= dataBase (self.base_name)
        dat1.read()
        global stick
        if str(self.id) in stick:
            # изменяем имеюoийся айди
            stick [stick[self.id]] = self.val  #======================эта строка================#
            print ('STICK ', stick, self.val)
            base = open(self.base_name, 'w')
            for k,v in stick.items() :
               if k == '0':
                   continue
               base.write(str(k +':'+v+'\n'))
            base.close()

        else:
            # добаляем айди
            base = open(self.base_name, 'a+')
            base_r = base.read()
            newT = base_r +str(id) + ' : ' + str(val)
            base.write(newT)
            base.close()

A
Alexander Pikeev, 2020-08-20
@Baryon

Youself.val = self.val

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question