python convert a csv to a tsv 1

python convert a csv to a tsv

import csv

with open('D:/AddressEvaluation/NAD/NAD.csv','r') as csvin, open('D:/NAD.txt', 'w') as tsvout:
    csvin = csv.reader(csvin)
    tsvout = csv.writer(tsvout, delimiter='\t')

    for row in csvin:
        tsvout.writerow(row)

Here is what the above code is Doing:
1. Open the input file in read mode and output file in write mode.
2. Create a csv.reader object from the input file (csv.reader parses a file into rows).
3. Create a csv.writer object for the output file (csv.writer will take a list of values and output a delimited string).
4. Loop through each row in the input file, and write it to the output file.

Similar Posts