简体   繁体   English

给定字符串中的代码参考

[英]Reference for code in given string

I'm running an automated messaging service that sends HTML and plain-text e-mails. 我正在运行一个自动消息传递服务,该服务发送HTML和纯文本电子邮件。 The script that I'm using will receive 3 parameters. 我正在使用的脚本将收到3个参数。 To, subject and a string that contains 4 different types of data. To,主题和包含4种不同类型数据的字符串。

The problem is, I can't figure out how to reference these four values seperately. 问题是,我无法弄清楚如何分别引用这四个值。 I've done a couple of attempts with sys.argv[3], which should display these given arguments. 我已经对sys.argv [3]进行了几次尝试,它应该显示这些给定的参数。 The question I have is, how do I get Python to recognize these as 4 different values, instead of one string? 我的问题是,如何让Python将它们识别为4个不同的值,而不是一个字符串? Because I to reference them in the e-mails I've written, which are written into the code itself. 因为我在写的电子邮件中引用了它们,所以将这些电子邮件写入代码本身。

 #!/usr/bin/env python

list1=[0,1,2,3,4];

import mimetypes, os, smtplib, sys, untangle
from email import encoders
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from email.utils import make_msgid

me = "xx@yy.nl"
you = "yyy@xx.nl"
DESTINATION=sys.argv[1]
SUBJECT=sys.argv[2]
MESSAGE=sys.argv[3]
PASS='yyyxxxyyy'
SRVSMTP='mysmtpsever:587'

msg = MIMEMultipart('alternative')
msg['Subject'] = SUBJECT
msg['From'] = me
msg['To'] = you
msg['Date'] = formatdate()
msg['Message-ID'] = make_msgid()

text = """
email with references here
"""
html = """
html email with references here 
"""

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

smtpserver = smtplib.SMTP(SRVSMTP)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(me, PASS)

smtpserver.sendmail(me, you, msg.as_string())
smtpserver.quit()

The string the monitoring system pastes is in BASH. 监视系统粘贴的字符串在BASH中。 What I had in mind is something the lines of XML's working, like this. 我想到的是XML的工作原理,就像这样。

<Root>
<Child1> {HOST.NAME} </Child1>
<Child2> {TRIGGER.NAME} </Child2>
<Child3> {TRIGGER.STATUS} </Child3>
<Child4> {TRIGGER.SEVERITY} </Child4>
</Root>

I feel like I'm thinking into the right direction, but I can't wrap my head around this. 我感觉自己正在朝着正确的方向思考,但是我无法解决这个问题。 Any advice is appreciated. 任何建议表示赞赏。

So since you said you can complete control the third parameter, I would suggest separating the four elements of the parameter with a symbol that will not occur in the elements. 因此,既然您说您可以完全控制第三个参数,那么我建议您用一个不会在元素中出现的符号来分隔参数的四个元素。 Often the semicolon ; 通常是分号; is used for that, but you can use any character you want. 用于,但是您可以使用任何想要的字符。 You would than get these elements with the following python line: 然后,您将使用以下python行获取这些元素:

element1, element2, element3, element4 = sys.argv[3].split(';')

If for some reason you want to use xml, you can do that: 如果出于某种原因要使用xml,则可以执行以下操作:

import xml.etree.ElementTree as ET
root = ET.fromstring(sys.argv[3])
child1, child2, child3, child4 = (c.text for c in root.children)

You can use this if you cant gurrantee that one character will not be in the text. 如果您不能保证一个字符不会出现在文本中,则可以使用它。

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

相关问题 输出给定字符串中第一个重复字符的代码? - Code to output the first repeated character in given string? 给定http链接的源代码中的grep字符串 - Grep string in the source code of a given http link 给定对 function 的引用,在签名中生成不带注释的源代码 - Given a reference to function, produce it's source code without annotations in the signature 返回字符串“code”出现在给定字符串中任意位置的次数 - Return the number of times that the string "code" appears anywhere in the given string 如何测试pandas数据框字符串列中的哪个单元格包含给定参考字符串的子字符串? - How to test which cell in a pandas dataframe string column contains a substring of a given reference string? Python 代码查找给定字符串中的最小字符数 - Python code to find minimum number of characters in a given string Python,使用print()和replace()函数来解释string给出的代码 - Python, using print() and replace() function to interpret code given by string 将给定 python 代码中的所有导入作为字符串列出 - list all the imports from the given python code as a string 在给定索引处将一些字符串插入给定字符串 - Insert some string into given string at given index 我正在为给定宽度包装一个字符串,但我的代码没有生成预期的 output - I am wrapping a string for a given width, but my code is not generating expected output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM