Turtle是Notation 3
語法的一個子集,以便rdflib應該能夠使用format='n3'
解析它。 檢查rdflib
是否保留評論(id
s在您的示例中的評論(#...
)中指定)。如果不是,如圖所示在你的榜樣,那麼你可以手動解析它的輸入格式很簡單:
import re
from collections import namedtuple
from itertools import takewhile
Entry = namedtuple('Entry', 'id name address phone')
def get_entries(path):
with open(path) as file:
# an entry starts with `#@` line and ends with a blank line
for line in file:
if line.startswith('#@'):
buf = [line]
buf.extend(takewhile(str.strip, file)) # read until blank line
yield Entry(*re.findall(r'<([^>]+)>', ''.join(buf)))
print("\n".join(map(str, get_entries('example.ttl'))))
輸出:
Entry(id='id1', name='Alice', address='USA', phone='12345')
Entry(id='id1', name='Jane', address='France', phone='78900')
將條目保存到數據庫:
import sqlite3
with sqlite3.connect('example.db') as conn:
conn.execute('''CREATE TABLE IF NOT EXISTS entries
(id text, name text, address text, phone text)''')
conn.executemany('INSERT INTO entries VALUES (?,?,?,?)',
get_entries('example.ttl'))
若要在Python中進行一些後處理,請按組編號:
import sqlite3
from itertools import groupby
from operator import itemgetter
with sqlite3.connect('example.db') as c:
rows = c.execute('SELECT * FROM entries ORDER BY id LIMIT ?', (10,))
for id, group in groupby(rows, key=itemgetter(0)):
print("%s:\n\t%s" % (id, "\n\t".join(map(str, group))))
輸出:
id1:
('id1', 'Alice', 'USA', '12345')
('id1', 'Jane', 'France', '78900')
是在問題中引用的一樣一個由標籤所引用的'ttl'? – 2013-03-02 07:20:02
什麼是TTL格式? – 2013-03-02 07:20:23
我認爲它的[Turtle-Terse RDF Triple Language](http://www.w3.org/TeamSubmission/turtle/) – Abhijit 2013-03-02 07:20:58