简体   繁体   English

如何使用命令执行python脚本

[英]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.

相关问题 使用system()命令从另一个python脚本执行时,如何访问python脚本中变量的值? - How to access the value of a variable in a python script when executed from another python script using system() command? 获取在脚本本身内部执行 python 脚本的命令 - Get the command that executed the python script inside the script itself 如何将命令行参数作为字符串传递给从C ++执行的嵌入式Python脚本? - how to pass command-line arguments as a string to an embedded Python script executed from C++? 如何将命令行参数传递给从Python执行的Perl脚本 - How to pass command-line parameters to a Perl script executed from Python 如何在python shell脚本中使用上次执行命令的返回码 - How to use last executed command's return code in python shell script 如何在Python中获取已执行命令的返回值 - How to get return value of a executed command in Python 如何在python中查找svn命令是否成功执行 - how to find if svn command executed successfully or not in python 如何用python获取shell中执行的命令&arguments - How to get the command & arguments that was executed in shell with python python脚本可从命令行运行,但无法从Web应用程序执行时运行 - python script works from command line but not when it is executed from webapplication 访问通过系统命令执行的R中python脚本的输出 - Access the output of python script in R executed through system command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM