I received this code from a colleague and I’m meant to use it to process 100csv files to extract values about a particle. The code looks like this:
import sys
import csv
# Usage
# skdata_decode.py [input file 1] [input file 2] ...
# (It is possible to use filedcard to specify input files.)
#
# loop over files (sys.argv[0] is the name of the
if (len(sys.argv)<2):
print("Usage : ",sys.argv[0], "[input file 1] [input file 2]...")
exit()
# Process each file
for input_file_name in input_files:
with open(input_file_name, "r") as input_file:
print("Processing", input_file_name)
data = csv.reader(input_file)
if (len(sys.argv)<2):
print("Usage : ",sys.argv[0], "[input file 1] [input file 2]...")
exit()
for input_file_name in sys.argv[1:]:
with open(input_file_name,"r") as input_file:
print("Processing",input_file_name)
data = csv.reader(input_file)
pmt_id=[]
q=[]
t=[]
x=[]
y=[]
z=[]
vertex=[]
momentum=[]
mass_list=[]
pid_list=[]
momentum_list=[]
parent_list=[]
status_list=[]
nhits = 0
npart = 0
for row in data:
if (row[0]=='#E'):
if ('Run #' not in row[1]):
Run_no = row[1]
Event_no = row[2]
Trig_ID = row[3]
Year = row[4]
Month = row[5]
Day = row[6]
Hoour = row[7]
Minute = row[8]
Second = row[9]
if (row[0]=='#V'):
if ('x' not in row[1]):
vertex.append(row[1])
vertex.append(row[2])
vertex.append(row[3])
if (row[0]=='#P'):
if ('ID' not in row[1]):
index_part = row[1]
pid_list.append(row[2])
mass_list.append(row[3])
momentum.append(row[4])
momentum.append(row[5])
momentum.append(row[6])
momentum_list.append(momentum.copy())
momentum.clear()
parent_list.append(row[7])
status_list.append(row[8])
npart = npart + 1
if (('#' not in row[0])&(len(row)>1)):
pmt_id.append(int(row[0]))
q.append(float(row[1]))
t.append(float(row[2]))
x.append(float(row[3]))
y.append(float(row[4]))
z.append(float(row[5]))
And the output when I run ‘Python3 skdata_decode.py 1ring-e*.csv’ on terminal is
‘C:Usersmyuser> python3 skdata_decode.py 1ring-e*.csv
Python’
How can I modify this code to make sure I can extract all the necessary values?
I thought the issue lied in the * so I tried to reformat it in terms of ‘glob’, but that didn’t change anything. I couldn’t run the script originally so I tried making an if statement to have an exit but that didn’t work either.
swavea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.