Skip to content

Instantly share code, notes, and snippets.

@JupyterJones
Created September 25, 2022 04:14
Show Gist options
  • Save JupyterJones/f23c5385ee85dbe85257b57b3fea4cd5 to your computer and use it in GitHub Desktop.
Save JupyterJones/f23c5385ee85dbe85257b57b3fea4cd5 to your computer and use it in GitHub Desktop.
Search Jupyter notebooks for a term then enter the results in an SQLITE database
import glob
import os.path
FILES = []
dir = '.'
files = glob.glob(os.path.join(dir, '*.ipynb'))
for file in files:
print (file)
FILES.append(file)
def insert(data):
import sqlite3
conn = sqlite3.connect("new.db")
conn.text_factory = str
c = conn.cursor()
c.execute("CREATE VIRTUAL TABLE IF NOT EXISTS PROJECT using FTS4 (input)")
c.execute("INSERT into PROJECT values (?)", (data,))
for row in c.execute("SELECT ROWID,* FROM PROJECT ORDER BY ROWID DESC LIMIT 1"):
print ("\nPOST VERIFIED:\n",row[0],row[1])
conn.commit()
conn.close()
return
# this works data = "this is a test"
# insert(data)
ALL =[]
# string to search in a directory of files
word = 'mask'
# uncomment below to use an imput term
#word = input("Searchterm: ")
def WORDin(word):
for filename in FILES:
with open(filename, 'r') as fp:
# read all lines in a list
lines = fp.readlines()
for line in lines:
# check if string present on a current line
if line.find(word) != -1:
if len(line)<100:
#print('The term '+word+' exists in '+filename+' .\nLineNumber:', lines.index(line),line.replace("\\n",""))
data ='The term '+word+' exists in '+filename+' .\nLineNumber:', lines.index(line),line.replace("\\n","")
ALL.append(data)
#convert the data to a string
dataout = str(data).replace("\\n",'')
insert(dataout)
word = 'mask'
WORDin(word)
for line in ALL:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment