Given a file
my_file.dat like this one:
A | B | C | D | E | F 1 | 5 | 9 | 5 | 1 | hello 2 | 6 | 8 | 4 | 0 | finxters 3 | 7 | 7 | 3 | 1 | learn 4 | 8 | 6 | 2 | 2 | python
How to convert this file to a CSV file of the following standard comma-separated values format:
A,B,C,D,E,F 1,5,9,5,1,hello 2,6,8,4,0,finxter 3,7,7,3,1,learn 4,8,6,2,2,python
Method 1: Pandas Read and Write CSV
You can convert a
.dat file to a CSV file in Python in four simple steps: (1) Install the Pandas library, (2) import the Pandas library, (3) read the CSV file as DataFrame, and (4) write the DataFrame to the file.
- (Optional in shell)
pip install pandas
import pandas as pd
df = pd.read_csv('my_file.txt', sep='\s+|\s+')
Here’s a minimal example:
import pandas as pd read_file = pd.read_csv('my_file.dat', sep='\s+\|\s+') read_file.to_csv ('my_file.csv', index=None)
Note that we used the regular expression
sep='\s+|\s+' to use any whitespace, followed by the
'|' symbol, followed by any whitespace as a separator between two CSV values. If you have a different separator string, you can define it here.
🌍 Related Tutorial: Python Pandas DataFrame
Method 2: CSV Module
You can use the
csv module to read a
.dat file and write it as a
.csv file in 5 steps:
- Install and import the
- Open the
.datfile in reading mode and the
.csvfile in writing mode.
- Create a
- Iterate over the rows in the
.datfile and reformat them to a list of values.
- Write the list of row values to the CSV and repeat for the next row until completion.
Here’s a concrete example:
import csv with open('my_file.dat', 'r') as dat_file: with open('my_file.csv', 'w', newline='') as csv_file: csv_writer = csv.writer(csv_file) for row in dat_file: row = [value.strip() for value in row.split('|')] csv_writer.writerow(row)
The resulting CSV file looks like this:
You can learn more about some of the features used in the code here:
Method 3: Vanilla Python
No library is needed to convert a
.csv to a
.dat in Python. Simply use the standard file handling functionality
file.write(), in addition to basic Python tricks to convert the data in the input file to comma-separated values.
with open('my_file.dat', 'r') as dat_file: with open('my_file.csv', 'w', newline='') as csv_file: for row in dat_file: row = [value.strip() for value in row.split('|')] csv_file.write(','.join(row) + '\n')
For example, in the code we used the
string.join() method to create a fully compatible CSV row.
🌍 Related Tutorial: Python Convert String to CSV File
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.