Chapter 113
parse faq
NotebookPython 3 (ipykernel)8 cells
In [8]python · cell 1
python
import io
import requests
import docxIn [24]python · cell 2
python
def clean_line(line):
line = line.strip()
line = line.strip('\uFEFF')
return line
def read_faq(file_id):
url = f'https://docs.google.com/document/d/{file_id}/export?format=docx'
response = requests.get(url)
response.raise_for_status()
with io.BytesIO(response.content) as f_in:
doc = docx.Document(f_in)
questions = []
question_heading_style = 'heading 2'
section_heading_style = 'heading 1'
heading_id = ''
section_title = ''
question_title = ''
answer_text_so_far = ''
for p in doc.paragraphs:
style = p.style.name.lower()
p_text = clean_line(p.text)
if len(p_text) == 0:
continue
if style == section_heading_style:
section_title = p_text
continue
if style == question_heading_style:
answer_text_so_far = answer_text_so_far.strip()
if answer_text_so_far != '' and section_title != '' and question_title != '':
questions.append({
'text': answer_text_so_far,
'section': section_title,
'question': question_title,
})
answer_text_so_far = ''
question_title = p_text
continue
answer_text_so_far += '\n' + p_text
answer_text_so_far = answer_text_so_far.strip()
if answer_text_so_far != '' and section_title != '' and question_title != '':
questions.append({
'text': answer_text_so_far,
'section': section_title,
'question': question_title,
})
return questionsIn [25]python · cell 3
python
faq_documents = {
'data-engineering-zoomcamp': '19bnYs80DwuUimHM65UV3sylsCn2j1vziPOwzBwQrebw',
'machine-learning-zoomcamp': '1LpPanc33QJJ6BSsyxVg-pWNMplal84TdZtq10naIhD8',
'mlops-zoomcamp': '12TlBfhIiKtyBv8RnsoJR6F72bkPDGEvPOItJIxaEzE0',
}In [27]python · cell 4
python
documents = []
for course, file_id in faq_documents.items():
print(course)
course_documents = read_faq(file_id)
documents.append({'course': course, 'documents': course_documents})Output
data-engineering-zoomcamp machine-learning-zoomcamp mlops-zoomcamp
In [29]python · cell 5
python
import jsonIn [32]python · cell 6
python
with open('documents.json', 'wt') as f_out:
json.dump(documents, f_out, indent=2)In [33]python · cell 7
python
!head documents.jsonOutput
[
{
"course": "data-engineering-zoomcamp",
"documents": [
{
"text": "The purpose of this document is to capture frequently asked technical questions\nThe exact day and hour of the course will be 15th Jan 2024 at 17h00. The course will start with the first \u201cOffice Hours'' live.1\nSubscribe to course public Google Calendar (it works from Desktop only).\nRegister before the course starts using this link.\nJoin the course Telegram channel with announcements.\nDon\u2019t forget to register in DataTalks.Club's Slack and join the channel.",
"section": "General course-related questions",
"question": "Course - When will the course start?"
},
{
In [ ]python · cell 8
python
