从python列表写入xml文件

从python列表写入xml文件,python,xml,Python,Xml,我有以下列表:“[TW',6.5',TS',5.0',core\u h',2.0',core\u er',2.0',trace\u height',5.0',trace\u er',5.0',trace\u tand',5.0',D',5.0',预浸料h',14.0',预浸料r',14.0',预浸料tand',14.0',14.0',粗糙度水平',22.0] 有没有一种方法可以将其转换为字符串并以filename.xml格式写入 这些变量及其值在哪里 如果不允许使用逗号,它们可以用下划线分隔您可

我有以下列表:“
[TW',6.5',TS',5.0',core\u h',2.0',core\u er',2.0',trace\u height',5.0',trace\u er',5.0',trace\u tand',5.0',D',5.0',预浸料h',14.0',预浸料r',14.0',预浸料tand',14.0',14.0',粗糙度水平',22.0]

有没有一种方法可以将其转换为字符串并以filename.xml格式写入 这些变量及其值在哪里


如果不允许使用逗号,它们可以用下划线分隔

您可以在列表切片中使用
zip

Ex:

s = ['TW', 6.5, 'TS', 5.0, 'core_h', 2.0, 'core_tand', 2.0, 'core_er', 2.0, 'trace_height', 5.0, 'trace_er', 5.0, 'trace_tand', 5.0, 'D', 5.0, 'prepreg_h', 14.0, 'prepreg_er', 14.0, 'prepreg_tand', 14.0, 'roughness_level', 22.0]
res = ""
for i,v in zip(s[::2], s[1::2]):
    res += "{0}_{1}-".format(i, v)

print(res)
TW_6.5TS_5.0core_h_2.0core_tand_2.0core_er_2.0trace_height_5.0trace_er_5.0trace_tand_5.0D_5.0prepreg_h_14.0prepreg_er_14.0prepreg_tand_14.0roughness_level_22.0
输出:

s = ['TW', 6.5, 'TS', 5.0, 'core_h', 2.0, 'core_tand', 2.0, 'core_er', 2.0, 'trace_height', 5.0, 'trace_er', 5.0, 'trace_tand', 5.0, 'D', 5.0, 'prepreg_h', 14.0, 'prepreg_er', 14.0, 'prepreg_tand', 14.0, 'roughness_level', 22.0]
res = ""
for i,v in zip(s[::2], s[1::2]):
    res += "{0}_{1}-".format(i, v)

print(res)
TW_6.5TS_5.0core_h_2.0core_tand_2.0core_er_2.0trace_height_5.0trace_er_5.0trace_tand_5.0D_5.0prepreg_h_14.0prepreg_er_14.0prepreg_tand_14.0roughness_level_22.0

对不起,你的问题不太清楚。如果我明白你的意思,你会没事的:

t


”、“.join(YourList)
”.join(YourList)
,在每个元素的每个字符之间添加一个逗号。你能发布格式为
variablename1-variablename2-variablename2-variablenamen.xml
的预期输出吗。谢谢!