简体   繁体   中英

lxml append array element python3.6

I am building the xml format.

I want to do something like this:

<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>

Now I can append all email into ValidationRequest. But I got the wrong format.

This is my code:

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

The response:

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'&lt;Email&gt;12345@gmail.com&lt;/Email&gt;\\n'b'&lt;Email&gt;564685@yahoo.com.hk&lt;/Email&gt;\\n'b'&lt;Email&gt;54321@yahoo.com.hk&lt;/Email&gt;\\n'</ValidationRequest>\n"

How can I keep <Email></Email> and post to ValidationRequest?

You had the right idea, there's just a lot there that's just moving stuff around. You can bring it down to this:

import lxml.etree
import lxml.builder

email = ['12345@gmail.com','564685@yahoo.com.hk','54321@yahoo.com.hk']

em = lxml.builder.ElementMaker()

the_doc = em.ValidationRequest(
    em.Username('admin'),
    em.Password('XBR'),
    *(em.Email(address) for address in email)
)

print(lxml.etree.tostring(the_doc, pretty_print=True).decode())

The .decode() is only there for it to print nicely. If you need the bytes , you can just leave that off.

Note this line in particular:

    *(em.Email(address) for address in email)

This loops over email , generating an Email element for each address as it goes, and then unpacking the resulting tuple (which is the result of the parentheses surrounding the generator) in to the constructor for the ValidationRequest element.

Output:

<ValidationRequest>
  <Username>admin</Username>
  <Password>XBR</Password>
  <Email>12345@gmail.com</Email>
  <Email>564685@yahoo.com.hk</Email>
  <Email>54321@yahoo.com.hk</Email>
</ValidationRequest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM