python 从KeepassX转换导出的XML文件,可以将其导入1Password(通过CSV逗号分隔文件)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 从KeepassX转换导出的XML文件,可以将其导入1Password(通过CSV逗号分隔文件)相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import re
from lxml import etree
import StringIO
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

file = open('1password.csv','w')

parser = etree.XMLParser(encoding='utf-8', recover=True)

tree = etree.parse('keepassx.xml', parser)

groups = tree.xpath('/database/group')

# for group in range(len(groups)):
# 	subgroup = groups[group].xpath('group')

# 	if (len(subgroup)):
# 		# print len(subgroup)
# 		for node in subgroup:
# 			print len(subgroup), groups[group].getchildren()[0].text, node[0].text
# 	else:
# 		print len(subgroup), groups[group].getchildren()[0].text, len(groups[group].xpath('entry'))

def getnodes(gr, num):
	subgroup	= gr.xpath('group')
	entry		= gr.xpath('entry')
	groupTitle 	= gr[0].text

	if (len(entry)):
		for item in entry:
			title 		= str(item[0].text)
			username 	= str(item[1].text)
			password 	= str(item[2].text)
			url 		= str(item[3].text)
			comment 	= str(item[4].text)

			# print 'Title:', item[0].text.encode("UTF-8")
			s = ('"'+title+'","'+url+'","'+username+'","'+password+'","'+comment+'"').replace('None','')
			file.write(s+'\n')

	if(len(subgroup)):
		for node in range(len(subgroup)):
			# print 'getnodes'
			getnodes(subgroup[node], node)

for group in range(len(groups)):
	getnodes(groups[group], group)

file.close()

以上是关于python 从KeepassX转换导出的XML文件,可以将其导入1Password(通过CSV逗号分隔文件)的主要内容,如果未能解决你的问题,请参考以下文章

100天精通Python—第35天:json标准库大总结文末送书两本

Python3分析sitemap.xml抓取导出全站链接

如何读取xml文件导出为excel文件

从电子表格转换为 xml 中的列表列表

从 Python 导出文件的 MS Access 大文件大小问题

eclipse maven 导出项目依赖的jar包