Nice, very nice. In the other part of the table I also have <rect> in following format:…
- User
Nice, very nice. In the other part of the table I also have in following format: -10
Extract them in a new list, take the ones with "-35" and "-10" as text separately.
Assistant
Alright, this is getting interesting! We're now diving into SVG-like elements (
<rect>and<text>) within your HTML.Here's the plan to extract data from
<rect>elements and their associated<text>(specifically<tspan>) content, and then create separate lists/tables in your TSV for entries where the text is "-10" and "-35":- Find
<rect>elements: Iterate through all<rect>tags. - Find Associated
<text>: For each<rect>, look for the immediately following<text>sibling element. - Extract Text from
<tspan>: If a<text>element is found, look for a<tspan>inside it and get its text content. - Extract
<rect>Attributes: Getwidth,height,x,y,fill,stroke-width, andstrokefrom the<rect>. - Filter and Group:
- If the extracted text is "-10", store the
<rect>attributes and the text in a list for "-10" items. - If the extracted text is "-35", store them in a list for "-35" items.
- If the extracted text is "-10", store the
- Format for TSV: Create new sections in the TSV file for these two groups, each with appropriate headers.
Here's the updated Python script:
import sys from bs4 import BeautifulSoup # Define input and output file paths html_file_path = 'table.html' tsv_file_path = 'table.tsv' try: # Read the HTML file with open(html_file_path, 'r', encoding='utf-8') as f: html_content = f.read() except FileNotFoundError: print(f"Error: Input file '{html_file_path}' not found.", file=sys.stderr) sys.exit(1) except Exception as e: print(f"Error reading file '{html_file_path}': {e}", file=sys.stderr) sys.exit(1) # Parse the HTML content try: soup = BeautifulSoup(html_content, 'lxml') except Exception as e: print(f"Error parsing HTML: {e}", file=sys.stderr) sys.exit(1) # --- Main data processing --- tsv_lines = [] # 1. First, capture standard HTML table data html_tables = soup.find_all('table') if html_tables: tsv_lines.append("# --- HTML Table Data ---") for i, table in enumerate(html_tables): if len(html_tables) > 1 and i > 0: # Add separator if more than one table tsv_lines.append(f"# --- Data from Table {i+1} ---") elif len(html_tables) > 1: # For the first table if multiple tsv_lines.append(f"# --- Data from Table {i+1} ---") rows = table.find_all('tr') for row in rows: cells = row.find_all(['td', 'th']) row_data = [] for cell in cells: cell_text = cell.get_text(separator=" ", strip=True) cell_text = cell_text.replace('\t', ' ').replace('\n', ' ').replace('\r', '') row_data.append(cell_text) tsv_lines.append('\t'.join(row_data)) if tsv_lines: tsv_lines.append("") # Add a blank line after table data if any table was processed else: print("No <table> elements found.", file=sys.stderr) # 2. Capture <datalist> elements and format them into a table datalists = soup.find_all('datalist') if datalists: if tsv_lines and tsv_lines[-1] != "": # Ensure a blank line if previous content exists tsv_lines.append("") tsv_lines.append("# --- Datalist Options as Table ---") headers = [] all_options_by_datalist = [] max_num_options = 0 for i, dl in enumerate(datalists): dl_id = dl.get('id', f"Datalist_{i+1}") headers.append(dl_id) current_dl_options = [] options_in_dl = dl.find_all('option') for opt in options_in_dl: cell_content = opt.get('value') if cell_content is None: cell_content = opt.get_text(strip=True) else: cell_content = cell_content.strip() cell_content = cell_content.replace('\t', ' ').replace('\n', ' ').replace('\r', '') current_dl_options.append(cell_content) all_options_by_datalist.append(current_dl_options) max_num_options = max(max_num_options, len(current_dl_options)) if headers: tsv_lines.append('\t'.join(headers)) for i in range(max_num_options): row_data = [ all_options_by_datalist[j][i] if i < len(all_options_by_datalist[j]) else '' for j in range(len(all_options_by_datalist)) ] tsv_lines.append('\t'.join(row_data)) if tsv_lines: tsv_lines.append("") # Add a blank line after datalist data else: print("No <datalist> elements found.", file=sys.stderr) # 3. Capture <rect> elements with specific associated text ("-10" or "-35") rects_data_minus_10 = [] rects_data_minus_35 = [] all_rect_elements = soup.find_all('rect') rect_item_headers = ["Rect Width", "Rect Height", "Rect X", "Rect Y", "Rect Fill", "Rect Stroke Width", "Rect Stroke", "Text"] for rect in all_rect_elements: text_element = rect.find_next_sibling('text') # Find text element immediately following the rect if text_element: tspan_element = text_element.find('tspan') if tspan_element: raw_text_content = tspan_element.get_text(strip=True) # Prepare data dictionary data_item = { "Rect Width": rect.get('width', ''), "Rect Height": rect.get('height', ''), "Rect X": rect.get('x', ''), "Rect Y": rect.get('y', ''), "Rect Fill": rect.get('fill', ''), "Rect Stroke Width": rect.get('stroke-width', ''), "Rect Stroke": rect.get('stroke', ''), "Text": raw_text_content # Store original text for filtering, clean later } # Clean all string values in the dictionary for key, value in data_item.items(): if isinstance(value, str): data_item[key] = value.replace('\t', ' ').replace('\n', ' ').replace('\r', '') # Filter based on the original (but stripped) text content if raw_text_content == "-10": rects_data_minus_10.append(data_item) elif raw_text_content == "-35": rects_data_minus_35.append(data_item) if not all_rect_elements: print("No <rect> elements found to process for text filtering.", file=sys.stderr) # Add "-10" rects to TSV if rects_data_minus_10: if tsv_lines and tsv_lines[-1] != "": tsv_lines.append("") tsv_lines.append("# --- Rects with Text \"-10\" ---") tsv_lines.append('\t'.join(rect_item_headers)) for item in rects_data_minus_10: row = [item.get(h, '') for h in rect_item_headers] # Use .get for safety tsv_lines.append('\t'.join(row)) if tsv_lines: tsv_lines.append("") # Add "-35" rects to TSV if rects_data_minus_35: if tsv_lines and tsv_lines[-1] != "": tsv_lines.append("") tsv_lines.append("# --- Rects with Text \"-35\" ---") tsv_lines.append('\t'.join(rect_item_headers)) for item in rects_data_minus_35: row = [item.get(h, '') for h in rect_item_headers] # Use .get for safety tsv_lines.append('\t'.join(row)) if tsv_lines: tsv_lines.append("") # --- Write the TSV output to a file --- if not tsv_lines: print("No data (tables, datalists, or relevant rects) found to write to TSV.", file=sys.stderr) try: with open(tsv_file_path, 'w', encoding='utf-8') as f: pass # Create empty file print(f"Created empty file '{tsv_file_path}'.") except Exception as e: print(f"Error creating empty output file: {e}", file=sys.stderr) sys.exit(1) else: # Remove trailing blank lines if any exist before writing while tsv_lines and tsv_lines[-1] == "": tsv_lines.pop() try: with open(tsv_file_path, 'w', encoding='utf-8') as f: f.write('\n'.join(tsv_lines)) if tsv_lines: # Add a final newline if content was written f.write('\n') print(f"Successfully converted data from '{html_file_path}' to '{tsv_file_path}'.") except Exception as e: print(f"Error writing output file '{tsv_file_path}': {e}", file=sys.stderr) sys.exit(1)Key changes for
<rect>and<text>processing:- Initialization:
rects_data_minus_10andrects_data_minus_35lists are created. - Header Definition:
rect_item_headersdefines the columns for these new TSV sections. - Iteration and Association:
soup.find_all('rect')gets all rectangle elements.rect.find_next_sibling('text')is crucial. It looks for the next sibling tag that is a<text>tag. This is how we associate a<rect>with its following text.text_element.find('tspan')then looks for the<tspan>within that<text>.
- Data Extraction & Cleaning:
- Attributes from
<rect>are fetched using.get('attribute_name', '')to provide a default empty string if an attribute is missing. - The text from
<tspan>is retrieved usingget_text(strip=True). - All extracted string values are cleaned (newlines and tabs replaced with spaces) after the raw text is used for filtering but before storing, to ensure they don't break the TSV structure.
- Attributes from
- Filtering: The
raw_text_content(stripped but not otherwise modified) is used for theif raw_text_content == "-10":andelif raw_text_content == "-35":conditions. - TSV Output:
- If data exists for "-10" or "-35" rects, new sections are added to
tsv_lines. - Each section starts with a comment header (e.g.,
# --- Rects with Text "-10" ---). - Then, the
rect_item_headersare written as a tab-separated string. - Finally, each collected data item is formatted into a tab-separated row.
- Added logic to ensure blank lines between sections and remove trailing blank lines at the end of the file.
- If data exists for "-10" or "-35" rects, new sections are added to
This should now correctly find your
<rect>elements, associate them with their subsequent text content (if structured as<rect>...</rect><text><tspan>...</tspan></text>), filter them, and output them into the specified separate lists in your TSV file.- Find
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.