编辑 2:对于那些想仔细查看代码的人,这里是: https ://github.com/pikzen/ffbookmark/blob/python-rewrite/ffbookmark.py
我在这里遇到了一些麻烦:每当我尝试将超过 204800 个字符写入文件时,python 都会引发 IOError。我在另一台计算机上尝试过,它以 768k 个字符崩溃。这是一个python问题,操作系统是否限制了什么?这是我正在使用的代码:
with open('out.json', 'w') as f:
json.dump(items, f)
items
是一个简单的字典。我从一个包含大约 800 个元素的 HTML 文件构建它。每个元素都是这样构建的:
bookmark = {}
bookmark["title"] = link.contents[0]
bookmark["id"] = id
bookmark["parent"] = 5
bookmark["dateAdded"] = 1
bookmark["lastModified"] = 1
bookmark["type"] = "text/x-moz-place"
uri = link.get('href')
# Shaarli's self links are totally messed up : ?xGRpkrp
# But we can't simply oust links containing '?'s, because
# php uses it, and pretty much everything does
# however, if there's not dot, we can assume it's a
# Shaarli link.
# If it's not, well too bad, false positive.
if "?" in uri and not '.' in uri:
bookmark['uri'] = "about:blank"
else:
bookmark['uri'] = uri
id += 1
try:
# This line messes up when the end of the file has been reached
# Rather than coding properly, let's just catch the exception
desc = link.parent.next_sibling.next_sibling
if desc and desc.name == "dd":
bookmark["annos"] = []
annos = {}
annos["name"] = "bookmarkProperties/description"
annos["flags"] = 0
annos["expires"] = 4
annos["mimeType"] = ""
annos["type"] = 3
annos["value"] = desc.contents[0]
bookmark["annos"].append(annos)
输出 :
IOError (Errno 27) : File too large
编辑:附加信息:Python 信息:
$ python --version
Python 2.7.3
使用的操作系统:
Linux Mint 13:限制:204.8kB
Debian 6.0:限制:768kB