简体   繁体   English

传递国家代码作为命令行参数输入

[英]Passing a country code as command line argument input

I have a program that gets the code of a country with raw_input() and then send an email based on it. 我有一个使用raw_input()获取国家/地区代码的程序,然后根据该代码发送电子邮件。

My code is: 我的代码是:

import pandas as pd

import pymysql

import sys

data=pd.read_CVS("filename")

country=raw_input("country name : ")

print("country name is :",sys.argv[1])

for index in data['emailid']:

    mail= index.split('@')[1]
    if(country == 'US'):
        if (mail=='yahoo.com'):
            print index
    elif(country == 'UK'):
        if(mail=='yahoo.com' or mail=='yahoo.co.uk'):
            print index

I want to change it to send the country code as command line argument input. 我想更改它以将国家/地区代码作为命令行参数输入发送。

Example: 例:

python programname.py 'UK'

and the related email ids as output. 以及相关的电子邮件ID作为输出。

raw_input reads from standard input whereas sys.argv is the list of command line arguments. raw_input从标准输入读取,而sys.argv是命令行参数的列表。 So if you want to use command line to pass in the country code, just delete the line containing raw_input and let country be sys.argv[1] . 因此,如果要使用命令行传递国家/地区代码,只需删除包含raw_input的行,并将countrysys.argv[1]

you should try argparse module, 您应该尝试使用argparse模块,

import argparse

parser = argparse.ArgumentParser(description='Example with non-optional arguments')    
parser.add_argument('country')  
args = parser.parse_args()
country = args.country

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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