2013-12-23 92 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)