Hello, everyone

the follow error shows when i try run my code
if (i==2): width = line[0,3]
TypeError: string indices must be integers

The code of error is this

for line in fl:
	        i += 1
	        if (i==2): width = line[0,3]
	        elif (i==3): frames = line[1,1]
	        elif (i>3): append(array_color, line)
	    fl.close()

Please, can someone explain me why this error occur? Thank you.

What are you appending to, and what does line look like? Use a test print of variable line.

You can only have one number inside a bracket. You want something like the following to get the fourth element, but only if it is a list of lists

print type(line[0]), line[0]
if (i==2): width = line[0][3]
##
##   or if you want the first three letters
if (i==2): width = line[0][:3]

Thank you
if (i==2): width = line[0][:3] is exactly what I needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.