HOWTO · Pandas
How to Load Data From Text File in Pandas
Learn how to load delimited, fixed-width, and tab-separated text files into pandas DataFrames with the read_csv(), read_fwf(), and read_table() functions, including guidance on separators, headers, and missing values.
On this page
A .txt extension does not determine how a file should be parsed. Use read_csv() for fields separated by a delimiter, read_fwf() for columns aligned at fixed character positions, and read_table() for tab-separated tables. Each reader returns a pandas DataFrame. The examples below create their own small input files so that each format is visible alongside the code.
Load delimited text with read_csv()
Use read_csv() when each record has fields separated by a consistent character, such as a comma or semicolon. By default, pandas treats the first row as column headers. Set header=None when the file has no header, and pass names to provide column labels. The example uses a comma-separated file with a header and one blank field:
from pathlib import Path
from tempfile import TemporaryDirectory
import pandas as pd
with TemporaryDirectory() as folder:
path = Path(folder) / "people.txt"
path.write_text("name,age\nAda,36\nLinus,", encoding="utf-8")
df = pd.read_csv(path, sep=",")
print(df.to_string(index=False))
name age
Ada 36.0
Linus NaN
Pandas recognizes common missing-value markers by default, including an empty field; use keep_default_na=False to disable those default markers, or na_values to add markers specific to your file. An empty age is displayed as NaN here, and the populated age is inferred as a floating-point value because the column also contains a missing value.
Load fixed-width text with read_fwf()
Use read_fwf() when fields occupy defined character positions and the records have no delimiter between columns. It is not a general reader for files whose delimiters vary. The example supplies the column widths explicitly so the boundaries are clear:
from pathlib import Path
from tempfile import TemporaryDirectory
import pandas as pd
with TemporaryDirectory() as folder:
path = Path(folder) / "people.txt"
path.write_text("Ada 036\nLinus 042\n", encoding="utf-8")
df = pd.read_fwf(path, widths=[6, 3], names=["name", "age"])
print(df.to_string(index=False))
name age
Ada 36
Linus 42
Adjust widths to match the character spans in your records; when boundaries are not known, inspect the file format before choosing widths. The supplied names label the two parsed fields, so the sample does not need a header row.
Load tabular text with read_table()
read_table() reads delimited data and uses a tab character as its default separator. Supply sep when the file uses another delimiter. This example uses the default tab separator:
from pathlib import Path
from tempfile import TemporaryDirectory
import pandas as pd
with TemporaryDirectory() as folder:
path = Path(folder) / "scores.txt"
path.write_text("name\tscore\nMina\t91\nOmar\t87\n", encoding="utf-8")
df = pd.read_table(path)
print(df.to_string(index=False))
name score
Mina 91
Omar 87
For a semicolon-delimited file, for example, pass sep=";"; the separator must match the characters in the file. Like read_csv(), read_table() uses the first row as headers by default, and header=None changes that behavior when the input begins directly with data.
Choose a reader and handle common options
Choose read_csv() for comma-separated or other consistently delimited records, read_fwf() for aligned fixed-width fields, and read_table() for tab-separated tables. For delimited readers, pandas infers a header from the first row unless you set header=None; use names=[...] to assign labels when the file has no header. Set na_values to identify file-specific missing-value markers. The read_fwf() example also accepts names, while its widths argument describes the fields’ character spans.
To load an existing .txt file, pass its path to the reader, such as pd.read_csv("/path/to/people.txt", sep=","). On Windows, a raw string such as r"C:\data\people.txt" avoids treating backslashes as escape characters. In each example, replace the temporary-file setup and path value with your own path, then keep the reader call and choose the separator, header, names, or widths that match the file. A relative path is resolved from the Python process’s current working directory, so use an absolute path if that location may vary.