
I am trying to access the first two letters of each index in a numpy array in python: I have read previous forum of the error "'int' object is not subscriptable , I know it;s not a string, but for my work it's better to be numpy.array or if anyone suggest me with another thing, please help,
Here is my code:
import numpy as np
import os
import os.path
with open('trial.dat', 'r') as f:
data = f.readlines()
data = [(d+' ')[:d.find('#')].rstrip() for d in data]
x=len(data[0])
x_1=eval(data[0])
y=np.concatenate(x_1)
print(type(y))
for i in range (x):
if y[i[:2]]=='IS': # expected to be IS andso on.depened on the index
print('ok-CHOICE ONE ')
elif y[i[:2]]=='AT':
print('NOTok ')
else:
print()
Data to be used in the .dat file:
[["IS-2","AT-3","IS-4"]] # TYPE OF GN
dataand your expected output.evalis very dangerous, but your data looks like valid JSON (when the comment is removed). You could usejson.loadsinstead.