based on the syntax below, is there a way I can include a if-else statement if the filename matches (e.g. Data/testfile.csv) and I need to skip the first row and first column to read the file?"
if len(sys.argv) != 2:
print("Usage: python test.py <filename>")
sys.exit(1)
inf = open(sys.argv[1])
data = np.array(
[list(map(float, s.strip().split(","))) for s in inf.readlines()]
)
CodePudding user response:
Thanks @StSav012 for the pointer. It led me to this solution as shown below.
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python testlearner.py <filename>")
sys.exit(1)
inf = open(sys.argv[1])
if (sys.argv[1] == 'Data/testfile.csv'):
data = np.array(
[list(map(float, s.strip().split(",")[1:])) for s in inf.readlines()[1:]]
)
else:
data = np.array(
[list(map(float, s.strip().split(","))) for s in inf.readlines()]
)
CodePudding user response:
Try something like this:
if len(sys.argv) != 2:
print("Usage: python test.py <filename>")
sys.exit(1)
with open(sys.argv[1]) as inf:
skip_columns = int(inf == 'Data/testfile.csv') # or another condition
skip_rows = int(inf == 'Data/testfile.csv') # or another condition
data = np.array(
[list(map(float, s.strip().split(",")[skip_columns:]))
for s in inf.readlines()[skip_rows:]]
)
By the way, numpy
has loadtxt
and genfromtxt
functions. They have skiprows
/skip_header
and usecols
parameters and might be of help.