2013-12-23 94 views
1

我需要生成的蟒蛇与数据poulated从CSV文件生成模板文件的csv数据蟒蛇输出文件

我有两个输入文件的XML文件:

一个CSV文件在data.csv包含的数据是这样的:

ID YEAR PASS LOGIN HEX_LOGIN 
14Z 2013 (3e?k<[email protected]}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,从CSV文件中的XML字段中的数据:

输出文件contentes:输出

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

<?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<[email protected]}l</pass> 
</security> 
</SecurityProfile> 

内容命名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> 

谢谢您的建议。

+0

对我来说看起来相当微不足道 - 使用'csv.DictReader' +标准字符串格式化就足够了。你有什么尝试? –

+0

那么你在努力与哪一点? –

+0

起初我以为我需要一些lke elementtree,所以我一直在用xml挣扎,但是Burhan Khalid的解决方案看起来像我一样。 –

回答

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。

+0

是的,我可以更改模板。这符合我的需求!谢谢! –

+0

你很受欢迎。 – selllikesybok

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)