SoFunction
Updated on 2025-03-02

How to read multiple separated files using Pandas

Use Pandas to read multiple separated files

If the first line of your text file is separated by commas, the rest are separated by tab

You need to use the read_csv function in Pandas and specify multiple delimiters using regular expressions.

1,2,3,4,5,6
a	b	c	d	e	f
z	x	c	v	b	n

Here is the code for how to read the file using Pandas:

import pandas as pd
 
# Read text file, specify multiple delimiters using regular expressions, and use the first row as the column namedf = pd.read_csv('', sep=r'[,\t]', engine='python', header=0)
 
# Print data frameprint(df)

The output result should be:

   1  2  3  4  5  6
0  a  b  c  d  e  f
1  z  x  c  v  b  n

The sep parameter here uses regular expressions[,\t], means that the delimiter can be a comma or a tab.

The engine parameter specifies the resolver's engine, here we have selected the resolver that comes with Python.

Finally, the header=0 parameter tells Pandas to use the first row as the column name.

Pandas reads TXT, data in txt is separated by spaces

You can use pandas' read_csv function to read data in TXT files.

When calling the read_csv function, you can use the sep parameter to specify the separator between the data.

For example:

If the data in the TXT file is spaced, you can call the read_csv function using sep=' '.

Here is an example

import pandas as pd
 
# Read data in TXT filedf = pd.read_csv('', sep=' ')
 
# Display the first 5 rows of data()

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.