python 参数列表_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.

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::PORT:<1>

mode tcp

bind : name

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

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.

解决方案

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

Updated Code for new requirement

#!/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值