DEV Community

I'm Just a Guy with a Computer
I'm Just a Guy with a Computer

Posted on

The delimiter and lineterminator Keyword Arguments in Python.

Let's Say you want to separate cells with a tab character instead of a comma and you want the rows to be double-spaced. enter the following on your interactive shell to excute practically.

import csv
csvFile = open('example.tsv','w',newline='')

csvWriter = csv.writer(csvFile, delimiter='\t', lineterminator='\n\n')
csvWriter.writerow(['Laliga','Premier-League','serie-A','Bundesliga'])


csvWriter.writerow(['eggs','bacon','ham'])


csvWriter.writerow(['mudyrk','Reece-James','cucurella','Malo-Gusto','pektovic','palmer'])


csvFile.close()
Enter fullscreen mode Exit fullscreen mode

This changes the delimiter and line terminator characters in your file. The delimiter is the character that appears between cells on a row. By default, the delimiter for a CSV file is a comma. The line terminator is the character that comes at the end of a row. By default, the line terminator
is a newline. You can change characters to different values by using the d­elimiter and lineterminator keyword arguments with csv.writer().
Passing delimeter='\t' and lineterminator='\n\n' changes the character between cells to a tab and the character between rows to two newlines.We then call writerow() three times to give us three rows when excuted the produces the following output:

Laliga  Premier-League  serie-A Bundesliga

eggs    bacon   ham

mudyrk  Reece-James cucurella   Malo-Gusto  pektovic    palmer

Enter fullscreen mode Exit fullscreen mode

Now that our cells are separated by tabs, we’re using the file extension.tsv, for tab-separated values.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Retry later