开发者

Concatenate values in a list (XML,Python)

i have trouble with my XML. What i have to do is concatenate values from a list that is extracted from an XML Example

<?xml version="1.0" encoding="UTF-8"?>
<locales>
<api-url>url</api-url>
<locale>
    <market>AE</market>
    <langu开发者_Python百科ages>
        <language>ar</language>
    </languages>
</locale>
<locale>
    <market>AM</market>
    <languages>
        <language>hy</language>
        <language>ru</language>
    </languages>
</locale>

What i do is.

inputXml  = lxml.etree.parse('xml')

market = inputXml.xpath('//locale/market/text()')

Lang =  inputXml.xpath('//locale[i]/languages//language/text()')

i have to concatenate the values from market and Lang like this -->AE_ar, AM_hy, AM_ru

i tried it with a for loop but it did not workout for me. i am getting like AE_ar,AE_ar,AE_ar, AM_hy,AM_hy,AM_hy. is there another way like using Xslt or something to do this in Python


import lxml.etree as ET
import io

content='''\
<?xml version="1.0" encoding="UTF-8"?>
<locales>
<api-url>url</api-url>
<locale>
    <market>AE</market>
    <languages>
        <language>ar</language>
    </languages>
</locale>
<locale>
    <market>AM</market>
    <languages>
        <language>hy</language>
        <language>ru</language>
    </languages>
</locale>
</locales>
'''
inputXml=ET.parse(io.BytesIO(content))

for locale in inputXml.xpath('//locale'):
    market = locale.xpath('market/text()')[0]
    for lang in locale.xpath('languages/language/text()'):
        print('{m}_{l}'.format(m=market,l=lang))

yields

AE_ar
AM_hy
AM_ru
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜