I am trying to run the following code on my command line. I need to add an argument parser for my *argv argument in my Splitter function. When I try to run the code as follows I get an error: TypeError: splitter() got an unexpected keyword argument '*argv'.
I am wondering if there is a more proper way to add thus type of argument? The purpose of the argument is to allow people using the function to add from 0-inf arguments for *argv and I know it is working the way I want it to. I just don't know how to parse the argument.
import pandas as pd
import numpy as np
import argparse
def arg_parse():
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--input_file", required = True)
parser.add_argument("-s", "--sep", required=True,)
parser.add_argument("-t", "--target_col", required=True)
parser.add_argument("-n", "--new_col", required = False, default = None)
parser.add_argument("-a", "--*argv", required = False, default = None)
args=parser.parse_args()
return vars(args)
def splitter(input_file, target_col, sep, new_col = None, *argv):
df = pd.read_csv(input_file)
df[target_col] = df[target_col].str.split(sep)
exploded = df.explode(target_col)
exploded[target_col].replace(r'^\s*$', np.nan, regex=True, inplace = True)
exploded.dropna(subset=[target_col], inplace=True)
if new_col == None:
return(pd.DataFrame(exploded[[target_col,*argv]]))
else:
exploded[new_col] = exploded[target_col]
return(pd.DataFrame(exploded[[new_col,*argv]]))
if __name__ == '__main__':
args = arg_parse()
print(splitter(**args))