TurtleNotation 3
sözdizimi bir alt kümesi böylece rdflibformat='n3'
kullanarak ayrıştırmak gerekir olduğunu. rdflib
yorumlarının korunup korunmadığını kontrol edin (id
s, yorumunuzda (#...
) belirtilmiştir).Değilse ve örnekte görüldüğü sonra bunu elle ayrıştırmak olabilir gibi giriş biçimi olarak basittir:
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'))))
Çıktı:
Entry(id='id1', name='Alice', address='USA', phone='12345')
Entry(id='id1', name='Jane', address='France', phone='78900')
bir db girişleri kaydetmek için:
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'da bazı sonradan işlemeye gerek duyarsanız, kimliğe göre gruplandırmak için:
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))))
Çıktı:
id1:
('id1', 'Alice', 'USA', '12345')
('id1', 'Jane', 'France', '78900')
söz konusu etiketi ile başvurulan aynı başvurulan 'ttl' mı? –
TTL formu nedir? –
Sanırım [Turtle - Terse RDF Üçlü Dil] (http://www.w3.org/TeamSubmission/turtle/) – Abhijit