使用jsaon加载将XML转换为CSV

ddrv8njm  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(105)

地狱所有,我是新的python,我有一个请求转换XML到CSV..可以任何1帮助我..这里的XML文本我有什么,我用下面的代码..但它的工作正常,只有当有没有在数据中的 curl 括号..但这些天我们得到 curl 括号中的数据..请建议我如何能做到这一点..
我在json.loads..中得到未公开的字符串错误。
XML

<?xml version="1.0" encodning="utf-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://wwww.w3.org/2002/XMLSchema">
    <soap:Body>
        <GRPtRes
            xmlns="testatatstip">
            <GRPtresult>
                <Dob>
                    <RData>[{"unid:1234,"test":"testst","enterdata":"tetst}dfd{", "restdata":"testagain"},{"unid:123456565,"test":"tesfdftst","enterdata":"teddtstdfd", "restdata":"testagadfdin"},{"unid:12343434,"test":"test43st","enterdata":"tetstdfd", "restdata":"testadfdgain"}]</RData>
                    <Nofpage>1</Nofpage>
                </Dob>
                <resp>good</resp>
                <succe>true</succe>
            </GRPtresult>
        </GRPtRes>
    </soap:Body>
</soap:Envelope>

字符串
谢谢你的支持
PK
我尝试了XML树,但没有运气

ugmeyewa

ugmeyewa1#

你可以用pandas来做这个,如果你的xml声明是正确的,请参阅4.3.3,我在这里纠正了它:

import pandas as pd
from json import loads, dumps
from pprint import pprint

xml="""<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://wwww.w3.org/2002/XMLSchema">
    <soap:Body>
        <GRPtRes
            xmlns="testatatstip">
            <GRPtresult>
                <Dob>
                    <RData>[{"unid:1234,"test":"testst","enterdata":"tetst}dfd{", "restdata":"testagain"},{"unid:123456565,"test":"tesfdftst","enterdata":"teddtstdfd", "restdata":"testagadfdin"},{"unid:12343434,"test":"test43st","enterdata":"tetstdfd", "restdata":"testadfdgain"}]</RData>
                    <Nofpage>1</Nofpage>
                </Dob>
                <resp>good</resp>
                <succe>true</succe>
            </GRPtresult>
        </GRPtRes>
    </soap:Body>
</soap:Envelope>"""

df = pd.read_xml(xml, xpath='.//xmlns:GRPtresult', namespaces={"xmlns": "testatatstip"})
df.to_csv('Test.csv')

result = df.to_json(orient="index")
parsed = loads(result)
dumps(parsed, indent=4)

pprint(parsed)

字符串
文件名:

,Dob,resp,succe
0,,good,True


输出量:

{'0': {'Dob': None, 'resp': 'good', 'succe': True}}

相关问题