BSD
嗨,伙计们,我写了一个程序,用argparse获取参数并将其存储在变量中,然后变量内容被用来传递给我想要创建的XML文件;gere是我的代码片段:
import argparse
from lxml import etree
if __name__ == '__main__':
my_parser = argparse.ArgumentParser()
my_parser.add_argument('U', type=str, action='store', help='Enter the account user name')
my_parser.add_argument('P', type=str, action='store', help='Enter the account password')
my_parser.add_argument('i', type=str, action='store', help='Enter the phone Number To Send
the SMS')
my_parser.add_argument('k', type=str, action='store', help='Enter the senders phone
number')
my_parser.add_argument('data', type=str, action='store', help='Enter the text for SMS')
my_parser.add_argument('e', type=str, action='store', help='send sms?')
args = my_parser.parse_args()
username = args.U
password = args.P
phone = args.i
reply = args.k
data = args.data
#print(args.U, args.P, args.i, args.k, args.data)
if args.e:
# Create the root element
page = etree.Element('sms')
# Make a new document tree
doc = etree.ElementTree(page)
# Add the subelements
pageElement1 = etree.SubElement(page, 'Account')
subelement2 = etree.SubElement(pageElement1, 'id').text = username
subelement3 = etree.SubElement(pageElement1, 'password').text = password
# Add the subelements for "Attributes" tree
pageElement2 = etree.SubElement(page, 'Attributes')
subelement4 = etree.SubElement(pageElement2, 'reference').text = '123'
subelement5 = etree.SubElement(pageElement2, 'replyPath').text = reply
# Save to XML file
doc.write('output.xml', xml_declaration=True, encoding='utf-8')
当我从CMD运行它时,它只运行第一部分,但不执行XML部分。
有人能帮忙吗?
感谢
根据我的说法,问题是当您运行命令时,您没有从命令提示符发送e的值。尝试使用以下命令:-
python your_filename.py e="your value"
或者您也可以使用以下命令:-
python your_filename.py --e "your value"
并打印出args.e 的值