寻觅生命中的那一片浅草......

Python处理keepass导出的xml文件

用keepass管理密码
现在有员工需要将用户名和密码导出来
格式如下
username password

我觉得最难的地方是根据Value取出来的list中,有空值,如果是空值,取其值或者类型时,都会提示
“AttributeError: ‘NoneType’ object has no attribute ‘nodeType’”
可以加if i.firstChild is None:continue进行判断,如果是空值则跳出本次循环
if i.firstChild is None:continue这个是非常pythonic的写法哦

程序

[python]
#!/bin/env python
import sys
from xml.dom import minidom
if len(sys.argv[0:]) != 2:
print “./keepass.py your xml file’s path”
sys.exit()

list=[]
xmldoc = minidom.parse(sys.argv[1])
reflist = xmldoc.getElementsByTagName(‘Value’)
for i in reflist[::-1]:
if i.firstChild is None:continue
list.append(i.firstChild.nodeValue)

for j in range(0,len(list),3):
print list[j]+” “+list[j+2]

[/python]

输出结果
lisi BiPqIcbHVfUDxaONKjNV
zhangsan h5LGhEYc3OASZf1VcGbD

keepass.xml

[xml]



KeePass

2010-10-18T06:08:30Z

2010-10-18T06:08:30Z

2010-10-18T06:08:30Z
365
2010-11-12T05:41:29Z
-1
-1

False
False
True
False
False

True
c8kwCXpGOE+MBy1jzCOT/g==
2010-10-18T06:08:30Z
AAAAAAAAAAAAAAAAAAAAAA==
2010-10-18T06:08:30Z
NHp/A6+c1kqAdPJrncQ5mQ==
Vgbin7aoeEuGRJXKiJk47g==




NHp/A6+c1kqAdPJrncQ5mQ==
keepass

48

2010-12-20T13:00:58Z
2010-12-20T13:00:52Z
2010-12-20T13:01:06Z
2010-12-18T10:04:30Z
False
3
2010-12-20T13:00:52Z

True

null
null
NMjEkhZudUiMAnPQOYQa+Q==

NMjEkhZudUiMAnPQOYQa+Q==
0





2010-12-20T13:01:18Z
2010-12-20T13:01:07Z
2010-12-20T13:01:18Z
2010-12-18T10:04:30Z
False
1
2010-12-20T13:01:07Z


Notes



Password
h5LGhEYc3OASZf1VcGbD


Title
张三


URL



UserName
zhangsan


True
0




b3KWP7kC+UmZnnSA4d0byg==
0





2010-12-20T13:01:31Z
2010-12-20T13:01:22Z
2010-12-20T13:01:31Z
2010-12-18T10:04:30Z
False
1
2010-12-20T13:01:22Z


Notes



Password
BiPqIcbHVfUDxaONKjNV


Title
李四


URL



UserName
lisi


True
0






9LVoDcHI9UquYgzy1tRXzg==
2010-11-12T05:49:57Z


gimxTDSBBkWdSIFORZUx0w==
2010-11-12T05:50:02Z


7/7Rd5KBT0CyY9O9A2RDpA==
2010-11-12T05:50:05Z


tPT9aQPrNkOM2mmj6kP3CA==
2010-11-16T02:58:07Z


gN/uXsDF206oc1g783tsLQ==
2010-11-16T02:58:11Z


jMYxMK3iakOWgPJGVz9lSg==
2010-11-22T08:55:46Z


j8UaFYCZbE2drG+gNysIwg==
2010-11-26T08:30:23Z


13QkVRyovk+9TNqxneYO5w==
2010-11-26T08:33:04Z


1+DFhfzAYUmL74gsRFzZNw==
2010-12-20T12:41:13Z




[/xml]

尚无评论

发表评论

2022年十二月
« 5月    
 1234
567891011
12131415161718
19202122232425
262728293031