convert_data
Transform data files between formats like CSV, JSON, and XLSX using pandas for analysis and processing in VisiData workflows.
Instructions
Convert data from one format to another using pandas.
Args: input_path: Path to the input data file output_path: Path for the output file output_format: Target format (csv, json, xlsx, etc.)
Returns: Success message or error details
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input_path | Yes | ||
| output_path | Yes | ||
| output_format | No |
Implementation Reference
- src/visidata_mcp/server.py:260-320 (handler)The convert_data tool handler implementation. This function converts data from one format to another using pandas, supporting CSV, JSON, Excel, and TSV formats. It loads data from the input file, determines the output format from the file extension or explicit parameter, and saves the data in the target format. Returns a JSON result with conversion details including row and column counts.
@mcp.tool() def convert_data(input_path: str, output_path: str, output_format: Optional[str] = None) -> str: """ Convert data from one format to another using pandas. Args: input_path: Path to the input data file output_path: Path for the output file output_format: Target format (csv, json, xlsx, etc.) Returns: Success message or error details """ try: import pandas as pd from pathlib import Path # Load data with pandas input_extension = Path(input_path).suffix.lower() if input_extension == '.csv': df = pd.read_csv(input_path) elif input_extension == '.json': df = pd.read_json(input_path) elif input_extension in ['.xlsx', '.xls']: df = pd.read_excel(input_path) elif input_extension == '.tsv': df = pd.read_csv(input_path, sep='\t') else: df = pd.read_csv(input_path) # Determine output format from extension if not specified if output_format is None: output_format = Path(output_path).suffix.lstrip('.') # Save in the new format if output_format == 'csv': df.to_csv(output_path, index=False) elif output_format == 'json': df.to_json(output_path, orient='records', indent=2) elif output_format in ['xlsx', 'xls']: df.to_excel(output_path, index=False) elif output_format == 'tsv': df.to_csv(output_path, sep='\t', index=False) else: # Default to CSV df.to_csv(output_path, index=False) output_format = 'csv' result = { "input_file": input_path, "output_file": output_path, "output_format": output_format, "rows_converted": len(df), "columns_converted": len(df.columns) } return json.dumps(result, indent=2) except Exception as e: return f"Error converting data: {str(e)}\n{traceback.format_exc()}" - src/visidata_mcp/server.py:260-260 (registration)The convert_data tool is registered as an MCP tool using the @mcp.tool() decorator at line 260.
@mcp.tool() - examples/demo.py:21-21 (helper)Import statement showing convert_data is imported from visidata_mcp.server module in the demo script.
convert_data, - examples/demo.py:119-123 (helper)Example usage of convert_data in the demo script, showing how it converts CSV data to JSON format.
print("6. Converting to JSON...") json_output = Path(__file__).parent / "sample_data.json" result = convert_data(str(sample_file), str(json_output)) print(f"Conversion result: {result}") print()