I want to print out a certain value from a list. Here is my code:
rdf_f = open("substrate.txt")
for line in rdf_f:
fields = line.split()
if len(fields) > 1:
x = fields[1]
print(x[2])
How can I correctly use the print() command to print out the 3rd value of x? Because I got an error:
IndexError: string index out of range
I know if x = [1,2,3,4,5,6], my code works. But here x is a perpendicular column. When I use print(x), the output is
0
1
2
3
4
5
6
7
8
9
10
0
1
2
3
4
5
6
7
8
9
10
...
print(x[2])withprint(x), what do you get?xis a string and not a list! Because you assign the second string of the listfiledsto it.x(print(x)) right before the error occurs?