mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-24 12:41:02 +02:00
Preserve original order
This commit is contained in:
parent
a3b3fdf411
commit
123cebad8e
1 changed files with 16 additions and 8 deletions
|
|
@ -42,11 +42,15 @@ def format_table_data(rows, table_name, output_format):
|
|||
return json.dumps({table_name: rows}, indent=2)
|
||||
|
||||
elif output_format == 'csv':
|
||||
# Get all unique field names
|
||||
fieldnames = set()
|
||||
# Get field names in order from first row, then add any missing ones
|
||||
fieldnames = list(rows[0].keys()) if rows else []
|
||||
# Add any additional fields from other rows that might be missing
|
||||
all_fields = set(fieldnames)
|
||||
for row in rows:
|
||||
fieldnames.update(row.keys())
|
||||
fieldnames = sorted(fieldnames)
|
||||
for field in row.keys():
|
||||
if field not in all_fields:
|
||||
fieldnames.append(field)
|
||||
all_fields.add(field)
|
||||
|
||||
# Create CSV string
|
||||
output = io.StringIO()
|
||||
|
|
@ -56,11 +60,15 @@ def format_table_data(rows, table_name, output_format):
|
|||
return output.getvalue().rstrip()
|
||||
|
||||
elif output_format == 'table':
|
||||
# Get all unique field names
|
||||
fieldnames = set()
|
||||
# Get field names in order from first row, then add any missing ones
|
||||
fieldnames = list(rows[0].keys()) if rows else []
|
||||
# Add any additional fields from other rows that might be missing
|
||||
all_fields = set(fieldnames)
|
||||
for row in rows:
|
||||
fieldnames.update(row.keys())
|
||||
fieldnames = sorted(fieldnames)
|
||||
for field in row.keys():
|
||||
if field not in all_fields:
|
||||
fieldnames.append(field)
|
||||
all_fields.add(field)
|
||||
|
||||
# Create table data
|
||||
table_data = []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue