从python中的模板文件和csv数据生成输出文件

时间:2013-12-23 11:09:58

标签: python xml csv elementtree

我需要使用python

中csv文件中的数据生成xml文件

我有两个输入文件:

一个名为data.csv的CSV文件,其中包含如下数据:

ID  YEAR    PASS    LOGIN   HEX_LOGIN
14Z 2013    (3e?k<.P@H}l    hex0914Z    F303935303031345A
14Z 2014    EAeW+ZM..--r    hex0914Z    F303935303031345A
.......

一个名为template.xml的模板文件

<?xml version="1.0"?>
<SecurityProfile xmlns="security_profile_v1">
<year></year>
<security>
<ID></ID>
<login></login>
<hex_login></hex_login>
<pass></pass>
</security>
</SecurityProfile>

我希望获得与csv数据文件中的行一样多的输出文件,每个输出字段名为YEAR_ID,其中包含来自xml字段中csv文件的数据:

输出文件contentes:

输出文件#1的内容命名为2013_0950014z:

<?xml version="1.0"?>
<SecurityProfile xmlns="security_profile_v1">
<year>2013</year>
<security>
<ID>14Z</ID>
<login>hex0914</login>
<hex_login>F303935303031345A</hex_login>
<pass>(3e?k<.P@H}l</pass>
</security>
</SecurityProfile>

输出文件#2的内容名为2014_0950014z:

<?xml version="1.0"?>
<SecurityProfile xmlns="security_profile_v1">
<year>2014</year>
<security>
<ID>14Z</ID>
<login>hex0914</login>
<hex_login>F303935303031345A</hex_login>
<pass>EAeW+ZM..--r</pass>
</security>
</SecurityProfile>

感谢您的建议。

2 个答案:

答案 0 :(得分:2)

您可以更改模板吗?如果是这样,我会采取以下措施使这更简单:

<?xml version="1.0"?>
<SecurityProfile xmlns="security_profile_v1">
<year>{year}</year>
<security>
<ID>{id}</ID>
<login>{login}</login>
<hex_login>{hex_login}</hex_login>
<pass>{pass}</pass>
</security>
</SecurityProfile>

然后,这样的事情会起作用:

import csv

input_file_name = "some_file.csv" #name/path of your csv file
template_file_name = "some_file.xml" #name/path of your xml template
output_file_name = "{}_09500{}.xml"

with open(template_file_name,"rb") as template_file:
    template = template_file.read()

with open(filename,"rb") as csv_file:
    my_reader = csv.DictReader(csv_file)
    for row in my_reader:
        with open(output_file_name.format(row["YEAR"],row["ID"]),"wb") as current_out:
            current.write(template.format(year=row["YEAR"],
                                          id=row["ID"],
                                          login=row["LOGIN"],
                                          hex_login=row["HEX_LOGIN"],
                                          pass=row["PASS"]))

如果您无法修改模板,或者想要将其作为XML而不是基本的字符串操作进行处理,那么它会更复杂一些。

修改

修改了使用csv.DictReader而不是csv.reader的答案。

答案 1 :(得分:0)

import csv
from collections import defaultdict

header = '<?xml version="1.0"?><SecurityProfile xmlns="security_profile_v1">\n'
footer = '\n</SecurityProfile>'
entry = '''<security>
              <ID>{0[ID]}</ID>
              <login>{0[LOGIN]}</login>
              <hex_login>{0[HEX_LOGIN]}</hex_login>
              <pass>{0[PASS]}</pass>
           </security>'''

rows = defaultdict(list)

with open('infile.csv') as f:
   reader = csv.DictReader(f, delimiter='\t')
   for item in reader:
      rows[reader['YEAR']].append(item)

for year,data in rows.iteritems():
   with open('{}.xml'.format(year), 'w') as f:
      f.write(header)
      f.write('<year>{}</year>\n'.format(year))
      for record in data:
          f.write(entry.format(record))
          f.write('\n')
      f.write(footer)