简体   繁体   中英

How to executed python script with command

I created a script in python which reads excel file and calculate date now i want to execute it with following command:

python your_script.py ./path/to/transactions.csv

My python script file code:

import csv

def csv_data(path):
    with open(path) as csv_file:
        readCSV = csv.DictReader(csv_file)

        for line in readCSV:
            col_from = line['from']
            col_to = line['to']
            col_ammount = line['amount']
            from_amount = int(float(col_ammount.lstrip('$').replace(',', ''))) - int(
                float(col_ammount.lstrip('$').replace(',', '')))
            to_amount = 0 + int(float(col_ammount.lstrip('$').replace(',', '')))
            print(col_from, '$'+str(from_amount), col_to, '$'+str(to_amount))

csv_data('transactions.csv')

You need the sys library and then you can access the argument with sys.argv[1].

import sys
csv_data(sys.argv[1])
import csv
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-f", "--file", required = True,
    help = "path of .csv file is stored")

args = vars(ap.parse_args())

file = joblib.load(args["file"])


def csv_data(path):
    with open(path) as csv_file:
        readCSV = csv.DictReader(csv_file)

        for line in readCSV:
            col_from = line['from']
            col_to = line['to']
            col_ammount = line['amount']
            from_amount = int(float(col_ammount.lstrip('$').replace(',', ''))) - int(
                float(col_ammount.lstrip('$').replace(',', '')))
            to_amount = 0 + int(float(col_ammount.lstrip('$').replace(',', '')))
            print(col_from, '$'+str(from_amount), col_to, '$'+str(to_amount))

csv_data('transactions.csv')

Open CMD and write python your_script.py --file path/file.csv

Let me know. I'm also new but I'm trying hard to learn..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM