Convert TXT file to CSV

A CSV file is a type of plain text file that uses specific delimiters to separate the columns within each row. Most commonly, these delimiters are commas, but semicolons and tabs can also be used depending on the complexity of the data and the regional settings. This guide will show you how to convert a text file to a CSV file.

Prepare Your Text File

Ensure your text file is organized in a way that each line corresponds to one row of data, and each piece of data within the line is separated by a specific character, like a space, tab, or a vertical bar. For example:

John Doe 1234 Apple St. Redtown
Jane Smith 5678 Orange Ave. Bluemont

Tools

There are several tools and methods you can use to convert a text file to CSV:

Spreadsheet Software (Excel, Google Sheets)

  • Open your spreadsheet application.
  • Import the text file using the “Import” feature.
  • Select “Delimited” and then choose the correct delimiter that matches your text file (spaces, tabs, etc.).
  • Once imported, the data should appear in columns according to the delimiter.
  • Save or export the file as CSV.

Text Editor (Notepad++, Sublime)

  • Open your text file in a text editor that supports regular expressions.
  • Use the “Find and Replace” feature to replace spaces or other delimiters with commas.
  • Save the file with a .csv extension.

Scripting (Python)

with open('data.txt', 'r') as file:
    lines = file.readlines()
    with open('data.csv', 'w', newline='') as csvfile:
        for line in lines:
            csvfile.write(','.join(line.split()) + '\n')

This script reads a text file, splits each line into parts (assuming spaces as delimiters), joins the parts with commas, and writes the result to a new CSV file.

Commabot

Just use our service at https://commabot.com/editor to convert your text file to CSV.

Verify the CSV File

After conversion, it’s important to open your CSV file in a text editor or spreadsheet software to ensure that the data is correctly formatted. Check for common issues like missing data, extra commas, or misaligned columns.