TurtleNotation 3
वाक्य रचना के एक सबसेट तो rdflibformat='n3'
का उपयोग कर इसे पार्स करने के लिए सक्षम होना चाहिए है। जांचें कि rdflib
टिप्पणियां संरक्षित करता है (id
एस आपके नमूने में टिप्पणियों (#...
) में निर्दिष्ट हैं)।यदि नहीं और इनपुट प्रारूप के रूप में अपने उदाहरण में दिखाया गया है तो आप इसे मैन्युअल रूप से पार्स सकता है के रूप में सरल है:
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'))
यदि आपको पाइथन में कुछ पोस्टप्रोसेसिंग की आवश्यकता है तो आईडी द्वारा समूह:
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' है? –
टीटीएल फॉर्म क्या है? –
मुझे लगता है कि इसकी [टर्टल - टेर्स आरडीएफ ट्रिपल लैंग्वेज] (http://www.w3.org/TeamSubmission/turtle/) – Abhijit