需求
strings.xml里的文本交给外国友人翻译,老板让我整理成表格数据
废话不说,直接上代码
import xml.dom.minidom
import csv
def main():
#打开xml文档
dom = xml.dom.minidom.parse('D:\\strings.xml')
# 获取根元素
root = dom.documentElement
# 获取string列表
resource = root.getElementsByTagName('string')
category = 'en_string'
for node in resource:
# 得到名称
key = node.getAttribute('name')
# 得到对应的值
value = node.firstChild.data
data = {}
data['name'] = key
data['value'] = value
# 写到表格
with open('{}.csv'.format(category), 'a', encoding='utf_8_sig', newline='') as f:
w = csv.writer(f)
w.writerow(data.values())
if __name__=='__main__':
main()