【问题标题】:Generate output files from template file and csv data in python在 python 中从模板文件和 csv 数据生成输出文件
【发布时间】:2023-03-15 16:39:01
【问题描述】:

我需要从 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 文件的数据:

输出文件内容:

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

<?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>

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

<?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>

感谢您的建议。

【问题讨论】:

  • 对我来说看起来很简单——使用csv.DictReader + 标准字符串格式就足够了。你试过什么?
  • 那么你在纠结哪一点?
  • 起初我认为我需要一些像 elementtree 一样的东西,所以我在 xml 上苦苦挣扎,但 Burhan Khalid 的解决方案在我看来很不错。

标签: python xml csv elementtree


【解决方案1】:

您可以更改模板吗?如果是这样,我将执行以下操作以使其更简单:

<?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,"r") as template_file:
    template = template_file.read()

with open(input_file_name,"r") as csv_file:
    my_reader = csv.DictReader(csv_file)
    for row in my_reader:
        with open(output_file_name.format(row["YEAR"],row["ID"]),"w") as current_out:
            current_out.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。

修复了打开输入 CSV 文件并写入输出的变量名称。删除了“二进制”模式文件操作。

【讨论】:

  • 是的,我可以更改模板。这符合我的需要!谢谢!
【解决方案2】:
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)

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2011-06-12
    • 2016-03-24
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多