Hey! I'm working with a data file with 5 columns. My aim is to read from a data file the first and last 20 lines and calculate the sums of first and last 20 values in 2nd and 3rd columns. However, this returns an error. The code now looks like this:
The error I get is this:
count=open("C:/../file.hst", "r") first=open("C:/../file.hst", "r") last=open("C:/../file.hst", "r") #Not sure why I have to open the same file three times to get readlines working, won't print if I just use one file (f.ex. "count" for all) line_count = 0 for line in data: if line!="\n": line_count +=1 print("Number of lines: ", line_count) #Prints number of lines in file so I know what value to put in lastlines, this command works firstlines = start.readlines() firstlines = firstlines[0:19] #Reads first 20 lines lastlines = end.readlines() lastlines = lastlines[1192:1212] #Reads last 20 lines #Aim to sum the first and last 20 values in second and third column d1first = np.sum(firstlines[:,1])/20 d2first = np.sum(firstlines[:,2])/20 sumfirst = d1first+d2first d1last = np.sum(lastlines[:,1])/20 d2last = np.sum(lastlines[:,2])/20 sumlast = d1last+d2last print("Average of first 20 (both detectors) :", sumfirst) print("Average of last 20 (both detectors) :", sumlast)
The error I get is this:
d1first = np.sum(firstlines[:,1]) TypeError: list indices must be integers or slices, not tupleI've been able to use np.sum(data[:,1]) without any problem if I read the whole file, but for some reason this command is not working when I pick specific rows. Should I somehow create a loop to sum until the 20th value (or last 20 values) in the file, or how can I fix this issue? Any help is greatly appreciated!