Answer the question
In order to leave comments, you need to log in
Error IndexError: list index out of range, but such an index exists!?
here is part of the code:
def read (self):
base = open(str(self.base_name), 'r')
base_r = base.readlines()
base.close()
dict_r = {}
tx=0
while tx <len(base_r) :
base_re = base_r[tx]
st1 = base_re.split (':')
print ('st - ',st1)
#print(str(st1[0]),str(st1[1][:-1]))
dict_r [st1[0]] = str(st1[1])[:-1]
tx+=1
Answer the question
In order to leave comments, you need to log in
Most likely an error in the text file. Perhaps the structure of the file is broken (for example, there is no colon in one line).
def read (filename):
base = open(filename, 'r')
base_r = base.readlines()
base.close()
dict_r = {}
tx=0
while tx <len(base_r) :
base_re = base_r[tx]
st1 = base_re.split (':')
print ('st - ',st1)
# print(str(st1[0]),str(st1[1][:-1]))
dict_r [st1[0]] = str(st1[1])[:-1]
tx+=1
print('*'*20)
print(dict_r)
read('1.txt')
st - ['hello', 'hello2\n']
st - ['hello9', 'hello235345\n']
st - ['hello8', 'hello275\n']
st - ['hello7', 'hello2432\n']
st - ['hello6', 'hello2873']
********************
{'hello': 'hello2', 'hello9': 'hello235345', 'hello8': 'hello275', 'hello7': 'hello2432', 'hello6': 'hello287'}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question