【发布时间】:2022-01-06 00:05:11
【问题描述】:
我正在构建 xml 格式。
我想做这样的事情:
<ValidationRequest>
<Username>admin@xgateinternal.com</Username>
<Password>XBR!t6YMeJbmsjqV</Password>
<Email>12345@gmail.com</Email>
<Email>564685@yahoo.com.hk</Email>
<Email>54321@yahoo.com.hk</Email>
</ValidationRequest>
现在我可以将所有电子邮件附加到 ValidationRequest。但我得到了错误的格式。
这是我的代码:
#!/usr/bin/python
import lxml.etree
import lxml.builder
email = ['12345@gmail.com','564685@yahoo.com.hk','54321@yahoo.com.hk']
E = lxml.builder.ElementMaker()
ValidationRequest = E.ValidationRequest
Username = E.Username
Password = E.Password
Email = E.Email
op = []
for a in email:
GG = Email(a)
op.append(lxml.etree.tostring(GG, pretty_print=True))
print (lxml.etree.tostring(GG, pretty_print=True))
print(op)
ed = ''.join(map(str, op))
print(ed)
the_doc = ValidationRequest(
Username('admin'),
Password('XBR'),
ed
)
print (lxml.etree.tostring(the_doc, pretty_print=True))
回应:
b'<Email>12345@gmail.com</Email>\n'
b'<Email>564685@yahoo.com.hk</Email>\n'
b'<Email>54321@yahoo.com.hk</Email>\n'
[b'<Email>12345@gmail.com</Email>\n', b'<Email>564685@yahoo.com.hk</Email>\n', b'<Email>54321@yahoo.com.hk</Email>\n']
b'<Email>12345@gmail.com</Email>\n'b'<Email>564685@yahoo.com.hk</Email>\n'b'<Email>54321@yahoo.com.hk</Email>\n'
b"<ValidationRequest><Username>admin</Username><Password>XBR</Password>b'<Email>12345@gmail.com</Email>\\n'b'<Email>564685@yahoo.com.hk</Email>\\n'b'<Email>54321@yahoo.com.hk</Email>\\n'</ValidationRequest>\n"
我如何保留
【问题讨论】: