2017-08-18 86 views
0

我想知道如何以簡單的方式將結構化的python列表轉換爲xml並將其存儲爲xml文件? 我可以跳過線,或與「無」裏面:如何將結構化的python列表轉換爲xml

[ ['None', None, None, None, None, None, 'False', 'None', 'None'], 
    ... 
     ['None', None, None, None, None, None, 'False', 'None', 'None'] 
    ] 

這是我的結構化列表看起來像:

[ [ 'This is the first case', 
    'CASE001', 
    'Linux', 
    '2017-08-18 06:45:58', 
    '2017-08-18 06:46:47', 
    '55', 
    'Pass', 
    [], 
    [ ['None', None, None, None, None, None, 'False', 'None', 'None'], 
    ... 
     ['None', None, None, None, None, None, 'False', 'None', 'None'] 
    ] 
    ], 
    [ 'This is the second case', 
    'CASE002', 
    'Linux', 
    '2017-08-18 07:45:58', 
    '2017-08-18 07:46:47', 
    '65', 
    'Pass', 
    [], 
    [ ['None', None, None, None, None, None, 'False', 'None', 'None'], 
    ... 
     ['None', None, None, None, None, None, 'False', 'None', 'None'] 
    ] 
    ], 
    ... 
] 

的XML我不需要所有的字段,但我不介意是否更容易解析所有領域!最進口是獲得一個XML文件,它應該是這樣的:

<case> 
    <header>This is the first case</header> 
    <name>CASE001</name> 
    <age>Linux</age> 
    <failedSince>0</failedSince> 
    <skipped>false</skipped> 
    <duration>55</duration> 
    <status>PASSED</status> 
</case> 
... 
+0

我建議你使用XML庫(例如作爲['etree'](https://docs.python.org/3/library/xml.etree.elementtree.html)包含在Python中)。如果遇到問題,請提供一個[最小,完整且可驗證的示例](https://stackoverflow.com/help/mcve) ,它重現您的問題並請求更具體的問題。 –

+0

謝謝,我找到了一個可行的解決方案,並會回答我的問題,以供所有其他人使用! – FotisK

回答

0

有了一些更多的閱讀我想,我需要有效的解決方案。

主要作用域是將上述結構化列表轉換爲具有特定字段和值的xml。

在下面的代碼是我的解決方案:

import pickle 
import pprint 

import xml.etree.cElementTree as ET 

suite = ET.Element('suite') 

TestSuiteResults = [] 

# Reading the file that is previously stored with pickle.dump the structured python list 
try: 
    fileread = open('results.log','rb') 
    TestSuiteResults = pickle.load(fileread) 
    fileread.close() 
except IOError as error_msg: 
    print 'I/O error(%s): %s'%(error_msg.errno, error_msg.strerror) 
    sys.exit(1) 

# Just do a printout to check the list 
pp = pprint.PrettyPrinter(indent=0) 
pp.pprint(TestSuiteResults) 

for s in TestSuiteResults: 
    # Here is the structure that my xml should have, see question text 
    case = ET.SubElement(suite, 'case') 
    age = ET.SubElement(case, 'age') 
    className = ET.SubElement(case, 'className') 
    durationCase = ET.SubElement(case, 'durationCase') 
    failedSince = ET.SubElement(case, 'failedSince') 
    name = ET.SubElement(case, 'name') 
    skipped = ET.SubElement(case, 'skipped') 
    status = ET.SubElement(case, 'status') 
    stderr = ET.SubElement(case, 'stderr') 
    stdout = ET.SubElement(case, 'stdout') 

    # put specific list values to specific xml fields 
    # list index 3 & 4 & 7 are not used 
    # the rest list index from 8...len(s) - 3 are also skipped 
    # only the last two are used again s[len(s) - 2] & s[len(s) - 1] 
    age.text = str(s[2]) 
    className.text = str(s[0]) 
    durationCase.text = str(s[5]) 
    failedSince.text = '0' 
    name.text = str(s[1]) 
    skipped.text = 'false' 
    status.text = str(s[6]) 
    # sometimes s[y] has no value so stderr field is set to '-' 
    if str(s[len(s) - 1]): 
     stderr.text = str(s[len(s) - 1]) 
    else: 
     stderr.text = '-' 
    # s[x] will be stored in field stdout 
    stdout.text = str(s[len(s) - 2]) 

tree = ET.ElementTree(suite) 
tree.write('list2etree.xml') 

TestSuiteResults的實施例:

[ 
0 [ 
    0 'This is the first case', 
    1 'CASE001', 
    2 'Linux', 
    3 '2017-08-18 06:45:58', 
    4 '2017-08-18 06:46:47', 
    5 '55', 
    6 'Pass', 
    [], 
    [ ['None', None, None, None, None, None, 'False', 'None', 'None'], 
    ... 
     ['None', None, None, None, None, None, 'False', 'None', 'None'] 
    ], 
    x 'path', 
    y 'notes' 
    ], 
1 [ 
    0 '...' 
    1 .... 

的list2etree.xml例如:

<suite> 
    <case> 
     <age>Linux</age> 
     <className>This is the first case</className> 
     <durationCase>55</durationCase> 
     <failedSince>0</failedSince> 
     <name>CASE001</name> 
     <skipped>false</skipped> 
     <status>Pass</status> 
     <stderr>notes</stderr> 
     <stdout>path</stdout> 
    </case> 
    ... 
</suite> 
相關問題