"how to convert .dat to .csv using python?" Code Answer
1
str.strip() removes leading and trailing characters from a string.
you want to split the lines on "|", then strip each element of the resulting list:
import csv
with open('file.dat') as dat_file, open('file.csv', 'w') as csv_file:
csv_writer = csv.writer(csv_file)
for line in dat_file:
row = [field.strip() for field in line.split('|')]
if len(row) == 6 and row[3] and row[4]:
csv_writer.writerow(row)
str.strip()
removes leading and trailing characters from a string.you want to split the lines on
"|"
, then strip each element of the resulting list: