Convert Basic Fixed-Width Text to CSV
Learn the fundamentals of converting fixed-width positional text into CSV format. Covers column definition, character slicing, and output formatting.
Basic Conversion
Detailed Explanation
Fixed-Width to CSV Basics
Fixed-width text stores each field at a specific character position. Unlike CSV where commas separate fields, here the position and length of each field define its boundaries.
Example Input
John Smith 30 New York
Jane Doe 25 London
Bob Johnson 42 Tokyo
Column Definitions
| Column | Start | Width |
|---|---|---|
| FirstName | 0 | 10 |
| LastName | 10 | 10 |
| Age | 20 | 4 |
| City | 24 | 16 |
CSV Output
FirstName,LastName,Age,City
John,Smith,30,New York
Jane,Doe,25,London
Bob,Johnson,42,Tokyo
How It Works
The converter reads each line and extracts substrings at the defined positions. For example, FirstName is characters 0-9, LastName is characters 10-19, and so on. Whitespace trimming removes trailing spaces from each extracted value.
Key Considerations
- Lines shorter than expected positions will produce empty or partial values for later columns
- The converter adds a CSV header row automatically using your column names
- Special characters (commas, quotes, newlines) in field values are properly escaped in the CSV output per RFC 4180
Use Case
Converting legacy system exports to modern CSV format for import into spreadsheets, databases, or data analysis tools like pandas or R.