"how to convert .dat to .csv using python?" Code Answer


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]:
By ShutUpMagda on September 3 2022
Only authorized users can answer the Search term. Please sign in first, or register a free account.