A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/how-to-read-text-files-with-pandas/ below:

How to Read Text Files with Pandas?

How to Read Text Files with Pandas?

Last Updated : 23 Jul, 2025

In this article, we will discuss how to read text files with pandas in Python. In Python, the Pandas module allows us to load DataFrames from external files and work on them. The dataset can be in different types of files.

Text File Used

Below are the methods by which we can read text files with Pandas:

Read Text Files with Pandas Using read_csv()

We will read the text file with pandas using the read_csv() function. Along with the text file, we also pass separator as a single space (‘ ’) for the space character because, for text files, the space character will separate each field. There are three parameters we can pass to the read_csv() function.

Syntax: 

Syntax: data=pandas.read_csv('filename.txt', sep=' ', header=None, names=["Column1", "Column2"])

Parameters:

Example 1

In this example, we are using read_csv() function to read the csv file.

Python
# Read Text Files with Pandas using read_csv()

# importing pandas
import pandas as pd

# read text file into pandas DataFrame
df = pd.read_csv("gfg.txt", sep=" ")

# display DataFrame
print(df)

Output:

Example 2

In this example, we will make the header filed equal to None. This will create a default header in the output. And take the first line of the text file as data entry. The created header name will be a number starting from 0.

Python
# Read Text Files with Pandas using read_csv()

# importing pandas
import pandas as pd

# read text file into pandas DataFrame and
# create header
df = pd.read_csv("gfg.txt", sep=" ", header=None)

# display DataFrame
print(df)

Output:

Example 3:

In the above output, we can see it creates a header starting from number 0. But we can also give names to the header. In this example, we will see how to create a header with a name using pandas.

Python
# Read Text Files with Pandas using read_csv()

# importing pandas
import pandas as pd

# read text file into pandas DataFrame and create 
# header with names
df = pd.read_csv("gfg.txt", sep=" ", header=None, 
                 names=["Team1", "Team2"])

# display DataFrame
print(df)

Output:

Read Text Files with Pandas Using read_table()

We can read data from a text file using read_table() in pandas. This function reads a general delimited file to a DataFrame object. This function is essentially the same as the read_csv() function but with the delimiter = '\t', instead of a comma by default. We will read data with the read_table function making separator equal to a single space(' ').

Syntax: data=pandas.read_table('filename.txt', delimiter = ' ')

Parameters:

Example: In this example, we are using read_table() function to read the table.

Python
# Read Text Files with Pandas using read_table()

# importing pandas
import pandas as pd

# read text file into pandas DataFrame
df = pd.read_table("gfg.txt", delimiter=" ")

# display DataFrame
print(df)
Output: Read Text Files with Pandas Using read_fwf()

The fwf in the read_fwf() function stands for fixed-width lines. We can use this function to load DataFrames from files. This function also supports text files. We will read data from the text files using the read_fwf() function with pandas. It also supports optionally iterating or breaking the file into chunks. Since the columns in the text file were separated with a fixed width, this read_fwf() read the contents effectively into separate columns.

Syntax: data=pandas.read_fwf('filename.txt')

Parameters:

Example: In this example, we are using read_fwf to read the data.

Python
# Read Text Files with Pandas using read_fwf()

# importing pandas
import pandas as pd

# read text file into pandas DataFrame
df = pd.read_fwf("gfg.txt")

# display DataFrame
print(df)

Output:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4