In the following program, I want to read in two input csv files from the command line. However, I don't know how my program knows that it should open the first csv file as mfpt_csv and the second as sbias_csv, since right now I'm using args.csv in both the opens.
parser = argparse.ArgumentParser()
parser.add_argument('csv', help='csv mfpt file')
parser.add_argument('csv', help='csv sbias file')
args = parser.parse_args()
def main():
with open(args.csv, 'r') as mfpt_csv, open(args.csv, 'r') as sbias_csv:
data = csv.reader(mfpt_csv, delimiter=',')
bits_from_csv = re.search(r"_([0-9]{3})_([0-9]{3}).csv$", args.csv)
for row in data:
if row[1] == bits_out:
inner_mfpt = row[2]
outer_mfpt = row[5]
if __name__ == '__main__':
main()
If I get rid of the add_argument ... 'csv sbias file' line and the second open, I don't have any problems running my program. However, if I include these lines, I get
Traceback (most recent call last):
File "../tools/matrix.py", line 33, in <module>
main()
File "../tools/matrix.py", line 25, in main
bits_in, bits_out = bits_from_csv.groups()
AttributeError: 'NoneType' object has no attribute 'groups'
which I think means that the program doesn't know from which csv file I'm supposed to read in my data. I don't know how to fix this problem.
question from:
https://stackoverflow.com/questions/65945636/python-arg-parser-how-to-deal-with-two-args-csv 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…