編集 2: コードを詳しく見てみたい方は、こちらをご覧ください: https://github.com/pikzen/ffbookmark/blob/python-rewrite/ffbookmark.py
ここで少し問題があります。204800 文字を超える文字をファイルに書き込もうとすると、Python が IOError をスローします。別のコンピューターで試したところ、768k 文字でクラッシュしました。それはPythonの問題ですか、OSが何かを制限していますか? 私が使用しているコードは次のとおりです。
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
使用OS:
Linux Mint 13: 制限: 204.8kB
Debian 6.0: 制限: 768kB