Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I handle CSV files that use a semicolon instead of a comma?

Quick answer

If you know the file uses semicolons between fields, tell Python’s CSV reader explicitly with delimiter=';'. Open the file with newline='', read its rows, and inspect the first few results and their column counts. If the delimiter is only a guess from a small sample, don’t treat that guess as confirmation; verify the format from a reliable description or the file’s producer.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I opened a CSV file and each row appears to be all in one column. The fields seem to be separated by semicolons, not commas. What should I change in Python?

Geez

If you know this file is semicolon-delimited, pass the separator explicitly to Python’s CSV reader. The default delimiter is a comma, so a semicolon file can otherwise be split incorrectly. For file objects, open with newline='' as the CSV documentation recommends. [S1]

import csv

with open("[input.csv]", newline="", encoding="utf-8") as f:
    reader = csv.reader(f, delimiter=";")
    rows = list(reader)

for row in rows[:5]:
    print(row)

Replace [input.csv] with your local filename. This example reads the rows into memory, which is convenient for a small file; for a large file, inspect rows as you iterate instead of collecting all of them.

Follow-up question

How do I know whether it worked? The first line has headers, and I want to make sure the columns did not stay together or split strangely.

Geez

Print the header and compare the number of fields in each row. If you already know how many columns the file should contain, use that as a check; otherwise, look for consistent row lengths and sensible header names. A quick inspection might look like this:

if not rows:
    print("The file has no rows")
else:
    print("Header:", rows[0])
    print("Column counts:", [len(row) for row in rows[:5]])

For example, if the expected header is ['name', 'city', 'note'], a row like ['Ari', 'Oslo', 'works; remotely'] has three fields. Don’t split each line on ';' yourself; inspect the parsed rows instead. This check is an aid, not proof that the header or every row matches your expectations.

Follow-up question

I’m not completely sure it is semicolon-delimited. Can Python detect the right separator from a few lines, or should I just try one?

Geez

Python’s CSV module provides Sniffer.sniff(sample) to guess a format from a sample. But a guess from a short or ambiguous sample is not confirmation. The documentation’s example samples 1024 characters and still calls for checking the format against reliable knowledge of the file and inspecting parsed rows. [S1]

If you have a reliable file specification or know how it was exported, use that to choose the delimiter explicitly. If you test a guess, treat it as provisional: check the header, several representative rows, and the expected number of fields before using the data. If the evidence conflicts, pause and consult the file’s export settings or its provider rather than silently accepting a guess.

Follow-up question

Can you give me a small reusable version that checks the header and flags rows with a different number of columns?

Geez

Here is a small pattern for a file whose first row is a header. Set expected_columns to the count you expect, or leave it as None to compare each data row with the header’s width:

import csv

expected_columns = 3  # Replace with your expected count, or use None.

with open("[input.csv]", newline="", encoding="utf-8") as f:
    reader = csv.reader(f, delimiter=";")
    header = next(reader, None)

    if header is None:
        print("No rows found")
    else:
        target = expected_columns or len(header)
        print("Header:", header)
        for line_number, row in enumerate(reader, start=2):
            if len(row) != target:
                print(f"Check row {line_number}: expected {target}, got {len(row)}")

This identifies rows worth reviewing; it does not establish that the chosen delimiter is correct or validate the meaning of each value. If your file has no header, choose an expected column count from a reliable description instead.

A practical checklist

  • Confirm the delimiter from a reliable file description or export setting when possible.

  • Open the file with newline='' and pass the known separator as delimiter=';'.

  • Inspect the header and several representative parsed rows.

  • Compare row lengths with the expected column count and review any mismatches.

  • Treat sample-based delimiter detection as a guess until verified.

A few more useful answers

Why does a semicolon file show up as one column?

The CSV reader uses a comma by default. If the file’s fields are separated by semicolons, specify delimiter=';' when creating the reader. [S1]

Should I use Python’s Sniffer instead of specifying the delimiter?

Use Sniffer only when the format is uncertain, and treat its result as a guess. Verify it against reliable information about the file and inspect parsed rows; a sample alone does not confirm the format. [S1]

What does `newline=''` do in the file-opening example?

It follows the CSV module’s documented guidance for opening file objects used with its reader. [S1]

Sources

References used for this guide.

  1. [S1] Python 3.14 csv — CSV File Reading and Writing
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work