Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

Python arg parser-- how to deal with two args.csv?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...