Calling Argument in Python code -


i have following code demands -p argument when calling. however, how call -p argument in sql query? looking use -p argument text in output file name.

#!/usr/bin/python  import argparse import psycopg2 import csv  parser = argparse.argumentparser(description='insert project id  argument') parser.add_argument('-p','--project_id', help='project_id pull files  erapro',required=true) args = parser.parse_args()  conn = psycopg2.connect(database="xxx", user="xxx", password="xxx",  host="xxx", port="5432") cur = conn.cursor()  cur.execute("""select project_analysis.project_accession,  analysis.analysis_accession, file.filename, file.file_md5, file.file_location     project_analysis     left join analysis on project_analysis.analysis_accession = analysis.analysis_accession     left join analysis_file on analysis.analysis_accession = analysis_file.analysis_accession     left join file on analysis_file.file_id = file.file_id     project_accession = <insert -p argument here> , analysis.hidden_in_eva = '0';""")  records = cur.fetchall()  open ('/nfs/production3/eva/user/gary/evapro_ftp/<insert -p argument  here>.csv', 'w') f: writer = csv.writer (f, delimiter = ',') row in records:     writer.writerow(row)  conn.close() 

all appreciated.

thanks

first assign argument variable using dest argument add_argument(). lets assign input project_id variable. way can reference in code.

parser.add_argument('-p','--project_id',                       help='project_id pull files                      erapro',                      required=true,                      dest='project_id') # notice dest argument  cur.execute("""select project_analysis.project_accession,  analysis.analysis_accession, file.filename, file.file_md5, file.file_location     project_analysis     left join analysis on project_analysis.analysis_accession = analysis.analysis_accession     left join analysis_file on analysis.analysis_accession = analysis_file.analysis_accession     left join file on analysis_file.file_id = file.file_id     project_accession = %s , analysis.hidden_in_eva = '0';""", (args.project_id)) 

notice use of execute(' ... %s ...', (args.project_id)) doing interpolated value referenced project_id string.


Comments