Sorry for the poorly worded title, it's the best I can do.
I have two lists, one that is a list of keywords, named breakpoints, and one that is a list of string values I created with BeautifulSoup pulled.
from bs4 import BeautifulSoup
from pymongo import MongoClient
client = MongoClient('connection string')
res = client['database']['collection'].find({"created": {"$gte": datetime(2022, 1, 1)}})
soup = BeautifulSoup(list(res)[0]['text'], 'html.parser') #text stores some html code for injection into a web page
string_list = []
string_dict = {}
for string in soup.stripped_strings:
string_list.append(string)
#string_list = ['foo', 'bar', 'data1', 'data2', 'baz', 'data3', 'data4', ... ,'eggs', 'dataX']
breakpoints = ['foo', 'bar', 'baz' ... 'eggs']
for b in range(len(breakpoints)):
for s in range(len(string_list)):
if breakpoints[b]==string_list[s]:
string_dict[breakpoints[b]] = [string_list[s]:string_list.index(breakpoints[b+1])]
the last line, is invalid syntax, what I would like as a final output is:
string_dict: {
"foo": [], #or just None or an empty string, anything to indicate 'no values'
"bar": ['data1', 'data2']
...
"eggs": ['dataX']
}
I know I still have to throw in some logic for when it reaches the last index of the breakpoints list, but that's fairly trivial. I'm having trouble figuring out how to get just the basic logic down for constructing the lists for the dict.
Appreciate any help.