pandas
data loading
txt files
data analysis
Python

Load data from txt with pandas

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In pandas, loading a .txt file usually means using read_csv, because the function is designed for delimited text in general, not just comma-separated files. The main work is choosing the right separator, header handling, encoding, and dtype options so the resulting DataFrame matches the real structure of the file.

The Simplest Case

If the text file is comma-separated, the basic call is:

python
1import pandas as pd
2
3df = pd.read_csv("data.txt")
4print(df.head())

Even though the file ends with .txt, pandas does not care about the extension. It cares about the file format.

Text Files with Other Delimiters

Many text files are tab-separated, pipe-separated, or whitespace-separated.

Tab-separated:

python
df = pd.read_csv("data.txt", sep="\t")

Pipe-separated:

python
df = pd.read_csv("data.txt", sep="|")

Whitespace-separated:

python
df = pd.read_csv("data.txt", sep=r"\s+")

Picking the correct sep argument is the most important step.

Files Without a Header Row

If the file has no column names, tell pandas not to treat the first row as headers.

python
df = pd.read_csv("data.txt", sep=",", header=None)
print(df.head())

You can also provide your own names:

python
1df = pd.read_csv(
2    "data.txt",
3    sep=",",
4    header=None,
5    names=["year", "month", "sales"]
6)

This is common when importing logs or machine-generated exports.

Parsing Dates and Types

Pandas can parse columns while loading:

python
1df = pd.read_csv(
2    "data.txt",
3    sep=",",
4    parse_dates=["created_at"],
5    dtype={"user_id": "int64", "status": "string"}
6)

Doing this up front is usually cleaner than loading everything as plain strings and converting later.

Reading Large Text Files

If the file is very large, load it in chunks:

python
for chunk in pd.read_csv("big_data.txt", sep=",", chunksize=10000):
    print(chunk.shape)

This is useful when the full file does not fit comfortably in memory.

Handling Missing Values and Encoding

Real text files often contain missing markers or non-default encodings.

python
1df = pd.read_csv(
2    "data.txt",
3    sep=",",
4    na_values=["NA", "NULL", "-"],
5    encoding="utf-8"
6)

If the file was produced on another system, encoding issues are often the next thing to inspect after the delimiter.

Fixed-Width and Messy Text Files

Not every .txt file is delimiter-based. Some files use aligned character columns instead. In those cases, read_fwf can be a better fit than read_csv.

python
df = pd.read_fwf("report.txt")
print(df.head())

For messy text sources, a good debugging habit is to inspect the first few raw lines before guessing parser options.

python
with open("data.txt", "r", encoding="utf-8") as f:
    for _ in range(3):
        print(repr(next(f)))

That usually reveals whether the file is comma-separated, tab-separated, fixed-width, or inconsistently spaced.

Performance and Validation

After loading, validate the shape and dtypes immediately:

python
print(df.shape)
print(df.dtypes)

This catches bad separators early. A file parsed with the wrong delimiter often appears as one giant string column, which is much easier to spot right after loading than later in the analysis pipeline.

A Complete Example

Suppose sales.txt looks like this:

text
1year,month,sales
22023,January,30
32024,February,45
42025,March,50

Then:

python
1import pandas as pd
2
3df = pd.read_csv("sales.txt")
4print(df)
5print(df.dtypes)

This gives you a ready-to-use DataFrame for analysis and plotting.

Common Pitfalls

One common mistake is assuming .txt implies a special pandas function. In practice, read_csv is still the normal tool.

Another issue is guessing the separator instead of checking the actual file structure.

A third pitfall is forgetting header=None for files that do not contain column names, which causes pandas to treat the first data row as headers.

Summary

  • Use pd.read_csv for most text-file loading tasks in pandas.
  • Set sep to match the real delimiter in the file.
  • Use header=None and names=[...] when the file has no header row.
  • Parse dates and dtypes during load when possible.
  • Use chunking, encoding, and missing-value options for large or messy text files.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.