Python多个用户参数到列表 [英] Python multiple user arguments to a list

查看:215
本文介绍了Python多个用户参数到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有话要感谢你们所有人如此出色的建议.现在一切都变得有意义了.对于错误的变量命名,我深表歉意.只是因为我想快速学习,而在编写最终脚本并添加自己的增强功能时,我不会进行此类练习,这些内容将在此处发布.

I've got not words to thank you all of you for such great advice. Now everything started to make sense. I apologize for for my bad variable naming. It was just because I wanted to quickly learn and I wont carry out such practices when I write the final script with my own enhancements which will be posted here.

我想通过将我们隔离的值(ip,端口和名称)传递到模板来进一步走下去.我尝试了一下,但是即使感觉很近也无法解决问题.我要构造的文本如下所示. (

I want to go an another step further by passing the values we've isolated (ip,port,and name) to a template. I tried but couldn't get it right even though I feel close. The text I want to construct looks like this. (

          Host Address:<IP>:PORT:<1>
          mode tcp
          bind <IP>:<PORT> name <NAME>

我已经在rahul提供的工作脚本中进行了尝试.(我已经按照stackexchange的规定编辑了原始代码.请也对此进行一次帮助.

I have tried this within the working script provided by rahul.(I've edited my original code abiding stackexchange's regulations. Please help out just this once as well. Many thanks in advance.

#!/usr/bin/python
import argparse
import re
import string

p = argparse.ArgumentParser()
p.add_argument("input", help="input the data in format ip:port:name", nargs='*')  
args = p.parse_args()
kkk_list = args.input 

def func_three(help):
    for i in help:
        print(i)

for kkk in kkk_list:
    bb = re.split(":|,", kkk) 
    XXX=func_three(bb)
for n in XXX:
    ip, port, name = n
    template ="""HOST Address:{0}:PORT:{1}
              mode tcp
              bind {0}:{1} name {2}"""
       sh = template.format(ip,port,name)
       print sh

原始职位:-

初学者在这里.我写了下面的代码,它并没有帮助我.

orignial post:--

Beginner here. I wrote the below code and it doesn't get me anywhere.

#!/usr/bin/python
import argparse
import re
import string

p = argparse.ArgumentParser()
p.add_argument("INPUT")  
args = p.parse_args()
KKK= args.INPUT
bb=re.split(":|,", KKK)

def func_three(help):
    for i in help:
        #print help
        return help

#func_three(bb[0:3])
YY = var1, var2, var3 = func_three(bb[0:3])
print YY

运行此脚本的方式应为"script.py:".即:script.py 192.168.1.10:80:string 172.25.16.2:100:string

The way to run this script should be "script.py :". i.e: script.py 192.168.1.10:80:string 172.25.16.2:100:string

如您所见,是否传递了一个参数,我没有问题.但是,当有更多参数时,我无法确定如何锻炼正则表达式并通过循环来完成此操作.

As you can see if one argument is passed I have no problems. But when there are more arguments I cant determine how to workout the regexes and get this done via a loop.

回顾一下,这就是我希望输出看起来像要继续进行的方式.

So to recap, this is how i want the output to look like to proceed further.

192.168.1.10
80
name1

172.25.16.2
100
name2

如果还有其他更好的方法可以实现这一目标,请随时提出建议.

If there are better other ways to achieve this please feel free to suggest.

推荐答案

请根据上下文命名您的变量.您将需要使用nargs=*来接受多个参数.我添加了更新的代码,下面将根据您的要求进行打印.

Please name your variable with respect to context. You will need to use nargs=* for accepting multiple arguments. I have added the updated code below which prints as you wanted.

#!/usr/bin/python
import argparse
import re
import string

p = argparse.ArgumentParser()
p.add_argument("input", help="input the data in format ip:port:name", nargs='*')  
args = p.parse_args()
kkk_list = args.input # ['192.168.1.10:80:name1', '172.25.16.2:100:name3']

def func_three(help):
    for i in help:
        print(i)

for kkk in kkk_list:
    bb = re.split(":|,", kkk) 
    func_three(bb)
    print('\n')

# This prints
# 192.168.1.10
# 80
# name1


# 172.25.16.2
# 100
# name3

更新代码以符合新要求

#!/usr/bin/python
import argparse
import re
import string

p = argparse.ArgumentParser()
p.add_argument("input", help="input the data in format ip:port:name", nargs='*')  
args = p.parse_args()
kkk_list = args.input # ['192.168.1.10:80:name1', '172.25.16.2:100:name3']


def printInFormat(ip, port, name):
    formattedText = '''HOST Address:{ip}:PORT:{port} 
                        mode tcp 
                        bind {ip}:{port} name {name}'''.format(ip=ip, 
                                                                port=port, 
                                                                name=name)
    textWithoutExtraWhitespaces =  '\n'.join([line.strip() for line in formattedText.splitlines()])
    # you can break above thing
    # text = ""
    # for line in formattedText.splitlines():
    #       text += line.strip()
    #       text += "\n" 

    print(formattedText)


for kkk in kkk_list:
    ip, port, name = re.split(":|,", kkk)

    printInFormat(ip, port, name)


# HOST Address:192.168.1.10:PORT:80
# mode tcp
# bind 192.168.1.10:80 name name1
# HOST Address:172.25.16.2:PORT:100
# mode tcp
# bind 172.25.16.2:100 name name3

这篇关于Python多个用户参数到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆