如何将结构化python列表转换为xml

时间:2017-08-18 15:36:42

标签: xml python-2.7 list

我想知道如何以简单的方式将结构化的python列表转换为xml并将其存储为xml文件? 我可以跳过内部带有'None'的行:

    [ ['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>
...

1 个答案:

答案 0 :(得分: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>