Here is a Python script that follows your instructions:
import re
def escape_sql_string(s):
# Escape single quotes by replacing ' with ''
return s.replace("'", "''")
def main():
import sys
if len(sys.argv) != 2:
print('Usage: script.py inputfile')
return
inputfile = sys.argv[1]
with open(inputfile, 'r', encoding='utf-8') as f:
lines = f.readlines()
lines = [line.strip() for line in lines]
N = int(lines[0])
questions = lines[1:]
# Remove any empty lines
questions = [line for line in questions if line]
# Prepare introduction insert statement
intro_qnr = 1
intro_shownr = 'NULL'
intro_testid = 35
intro_question = 'Überprüfen Sie folgende Aussagen und antworten Sie mit „richtig“ oder „falsch“.\nWir wünschen Ihnen viel Erfolg!'
intro_question_escaped = escape_sql_string(intro_question)
intro_answers = 'NULL'
intro_correctanswer = 'NULL'
intro_breakafter = 'TRUE'
insert_statements = []
# Introduction insert statement
insert_statements.append(f"({intro_qnr}, {intro_shownr}, {intro_testid}, '{intro_question_escaped}', {intro_answers}, {intro_correctanswer}, {intro_breakafter})")
# Process questions
qnr = 2
shownr = 2
total_questions = len(questions)
question_pattern = r'^(\d+)\.\s*(.+?)\s*\(([^)]+)\)$'
for i, line in enumerate(questions):
m = re.match(question_pattern, line)
if not m:
print(f"Line does not match expected format: {line}")
continue
number = m.group(1)
question_text = m.group(2)
correct_answer = m.group(3)
answers = 'richtig\\falsch'
# Escape strings
question_text_escaped = escape_sql_string(question_text)
correct_answer_escaped = escape_sql_string(correct_answer)
# Determine breakafter
if ((qnr - 2) % N == N - 1) or (i == total_questions - 1):
breakafter = 'TRUE'
else:
breakafter = 'FALSE'
values_line = f"({qnr}, {shownr}, {intro_testid}, '{question_text_escaped}', '{answers}', '{correct_answer_escaped}', {breakafter})"
insert_statements.append(values_line)
qnr +=1
shownr +=1
# Output the SQL
print('INSERT INTO `tquestion` (`qnr`, `shownr`, `testid`, `question`, `answers`, `correctanswer`, `breakafter`) VALUES')
for i, stmt in enumerate(insert_statements):
if i == len(insert_statements) - 1:
# Last line
print(stmt + ';')
else:
print(stmt + ',')
if __name__ == "__main__":
main()
How to use this script:
-
Save the script to a file, for example generate_sql.py.
-
Prepare your input text file (e.g., questions.txt) with the specified format. The first line should be the number of questions per page, followed by the questions. Here's an example based on your data:
5
1. Ein Kompromiss ist der bestmögliche Ausgang einer Verhandlung. (falsch)
2. Reduzieren Sie Ihren eigenen Redeanteil und hören Sie in einer Verhandlung aktiv zu. (richtig)
3. Eine professionelle Verhandlungsagenda enthält stets den Punkt „Sonstiges“. (falsch)
-
Run the script with the input file as an argument:
python generate_sql.py questions.txt
What the script does:
- Reads the number of questions per page from the first line of the input file.
- Skips the second line (if it's an example or not needed).
- Processes the first question as an introduction, which stays on its own page.
- Parses each subsequent question, extracting the question text and the correct answer.
- Generates
answers as 'richtig\falsch' for each question.
- Sets the
breakafter value according to whether the question is at the end of a page or the last question.
- Outputs the SQL INSERT statements, properly formatted and with escaped strings to handle special characters.
Note:
- Ensure that your input file is saved with UTF-8 encoding to handle any special characters.
- The script assumes that the
testid is fixed at 35 as in your example. If this should vary, you can modify the script accordingly.
Feel free to modify the script if you need to adjust any of the hardcoded values or if you need to handle different input formats.