简体   繁体   中英

Outputting a list to a tsv file in python

This seems simple, but I couldn't find an answer. How do I go about outputting a flat list to a tsv file in python?

Using Python's CSV library with a \\t delimiter as follows:

import csv        

data = ['text1', 'text2', 'text3', 'text4']    

with open('output.tsv', 'w', newline='') as f_output:
    tsv_output = csv.writer(f_output, delimiter='\t')
    tsv_output.writerow(data)

Giving you output.tsv containing one row as:

text1   text2   text3   text4

If you are using Python 2.x, use:

with open('output.tsv', 'wb') as f_output:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM