xml2txt todolater

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os

def xml2txt(xml_path, txt_path, txt_name):
    files = os.listdir(xml_path)
    txt_path_cur = txt_path + txt_name
    txt_op = open(txt_path_cur, "w")
    for file in files:
        xml_file_path = xml_path + file
        file_op = open(xml_file_path,'r')
        for line in file_op.readlines():
            if "<module_name>" in line:
                module_name_true = line[13:-14]
                txt_op.write("%s\n" % module_name_true)
            elif "<sub_module_name>" in line:
                sub_module_name_true = line[17:-18]
                txt_op.write("%s\n" % sub_module_name_true)
            else:
                continue
        file_op.close()
    txt_op.close()


if __name__ == "__main__":
    xml_path_real = "D:\\code\\test\\"
    txt_path_real = "D:\\code\\test\\"
    txt_name_real = "txt_test.txt"
    xml2txt(xml_path_real, txt_path_real, txt_name_real)




posted on 2017-04-24 06:06  ramboly  阅读(194)  评论(0)    收藏  举报