[英]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中创建了一个脚本,该脚本读取excel文件并计算日期,现在我想使用以下命令执行该脚本:
python your_script.py ./path/to/transactions.csv
My python script file code: 我的python脚本文件代码:
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]. 您需要sys库,然后可以使用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
打开CMD并编写
python your_script.py --file path/file.csv
Let me know. 让我知道。 I'm also new but I'm trying hard to learn..
我也很新,但是我正在努力学习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.