Help me fix my Python code formating the inserts as they should be, this is my current…
- User
Help me fix my Python code formating the inserts as they should be, this is my current code that isn't formatting the columns to be aligned:
def format_insert(insert_block): # Remove unnecessary leading and trailing whitespace before processing insert_block = insert_block.strip()
# Check for valid insert statement if not insert_block.lower().startswith("insert") or 'values' not in insert_block.lower(): return insert_block # Separate the statement into the columns and values sections try: # Extracting the part of the line before "values" for columns columns_part = insert_block[insert_block.index('(') + 1:insert_block.index(')')].strip() values_part = insert_block[insert_block.lower().index('values') + len('values'):].strip() # Prepare columns and values lists columns = [col.strip() for col in columns_part.split(',')] values = [val.strip().strip('()') for val in values_part.split(',') if val.strip()] except ValueError: return insert_block # Return original if there is an error in parsing # If there are 3 or fewer columns, return unformatted if len(columns) <= 3: return insert_block # Create formatted output for columns formatted_columns_lines = [] for i in range(0, len(columns), 3): formatted_columns_lines.append(", ".join(columns[i:i + 3])) # Create formatted output for values formatted_values_lines = [] for i in range(0, len(values), 3): formatted_values_lines.append(", ".join(values[i:i + 3])) # Combine back into a single formatted insert statement formatted_insert = ( f"INSERT INTO some_table (\n" + ",\n".join(f" {line}" for line in formatted_columns_lines) + f"\n)\nVALUES (\n" + ",\n".join(f" {line}" for line in formatted_values_lines) + f"\n)" ) return formatted_insertExample SQL query for testing
query = """INSERT INTO students (name, age, grade, major, enrollment_date, email, phone) VALUES ('Alice', 21, 'A', 'Mathematics', '2022-09-01', 'alice@example.com', '555-1234'), ('Bob', 22, 'B', 'Physics', '2021-09-01', 'bob@example.com', '555-5678'), ('Charlie', 23, 'C', 'Chemistry', '2020-09-01', 'charlie@example.com', '555-8765');"""
formatted_query = format_insert(query) print(formatted_query)
here is the ouput of this code:
INSERT INTO some_table ( name, age, grade, major, enrollment_date, email, phone ) VALUES ( 'Alice', 21, 'A', 'Mathematics', '2022-09-01', 'alice@example.com', '555-1234', 'Bob', 22, 'B', 'Physics', '2021-09-01', 'bob@example.com', '555-5678', 'Charlie', 23, 'C', 'Chemistry', '2020-09-01', '', '555-8765'); )
- Assistant
- User
that only moved the commas not the names:
INSERT INTO some_table ( name, age, grade, major, enrollment_date, email, phone ) VALUES ( 'Alice', 21 , 'A' , 'Mathematics', '2022-09-01' , 'alice@example.com', '555-1234', 'Bob', 22 , 'B' , 'Physics', '2021-09-01' , 'bob@example.com', '555-5678', 'Charlie', 23 , 'C' , 'Chemistry', '2020-09-01' , 'charlie@example.com', '555-8765'); )
- Assistant
- User
the code you posted doesn't have anything to do to the output you think your code do, this is what I am getting:
INSERT INTO some_table ( name, age, grade, major, enrollment_date, email, phone ) VALUES ( 'Alice', 21 , 'A' , 'Mathematics', '2022-09-01' , 'alice@example.com', '555-1234', 'Bob', 22 , 'B' , 'Physics', '2021-09-01' , 'bob@example.com', '555-5678', 'Charlie', 23 , 'C' , 'Chemistry', '2020-09-01' , 'charlie@example.com', '555-8765'); )
- Assistant