TypeError: 'float' object is not subscriptable solved

It's actually a small problem, but it was also very baffling when it came out. Because everything ran well before, the problem only occurred when I changed different files to run. The key is that the content and format to be processed by different files are exactly the same. If one runs successfully, the other will report TypeError: 'float 'object is not subscriptable' error is very speechless. The next step is to see how to solve the problem:

Naturally, at the beginning, I directly searched for the problem and looked for solutions. To be honest, I read some blogs, but I didn’t directly find the solution, but I got some ideas from them and understood the problem more clearly. The following is an explanation found:
Insert image description here
Then let me talk about my specific application:
I need to get the prediction score and prediction position (coordinate value) from the json file generated by the prediction and write them into a txt document:

ff = open('xx.txt','w')
for anno in annos:
	score = anno['score']
	left,top,right,bottom = anno['box']
	ff.write("%s %s %s %s %s\n" %(score[:6], str(int(left)), str(int(top)),str(int(right)),str(int(bottom))))
ff.close()
	

json file content format:

{
    
    
	"annos":[
		{
    
    
			"score":0.999976544,
			"box":[
				145.436234753274657
				456.312645236453657
				254.327564356457435
				553.957943578765678
				]
		},
		{
    
    
			"score":0.9996542376,
			"box":[
				344.436234753274657
				987.312645236453657
				423.327564356457435
				234.957943578765678
				]
		},
		...
	]
}

The specific problem occurs when writing:

ff.write("%s %s %s %s %s\n" %(score[:6], str(int(left)), str(int(top)),str(int(right)),str(int(bottom))))

At the beginning, my focus was on reading the content of the box. It is four floating point numbers located under a list, and the method found is also related to the content of the list.

Later, after testing my control variables, I found that there was no problem here, so the problem was that the writing of score[:6] reported an error.

My solution is:

"""先把读取到的score转化为字符,再进行写入"""
xx = str(anno['score'])
ff.write("%s %s %s %s %s\n" %(xx[:6], str(int(left)), str(int(top)),str(int(right)),str(int(bottom))))

The above can be executed smoothly without reporting TypeError: 'float' object is not subscriptable

I originally thought about adding str directly to the last sentence, that is:

ff.write("%s %s %s %s %s\n" %(str(score[:6]), str(int(left)), str(int(top)),str(int(right)),str(int(bottom))))

It is found that an error will still be reported, which means that the floating point data cannot be read when writing directly ( it may be that the floating point data read directly from the annos list cannot be processed directly ), even after intermediate processing. It needs to be converted into character type and then written to solve the problem.

In fact, this problem is more like a bug , and it is not a problem that will definitely occur . If an error is reported,
it will take time to solve it.

Guess you like

Origin blog.csdn.net/qq_44442727/article/details/126479409