python - 在python中将列表输出到tsv文件

标签 python list csv

这看起来很简单,但我找不到答案。如何在 python 中将平面列表输出到 tsv 文件?

最佳答案

使用带有 \t 分隔符的 Python 的 CSV 库如下:

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)

给你 output.tsv 包含一行:

text1   text2   text3   text4

如果您使用的是 Python 2.x,请使用:

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

关于python - 在python中将列表输出到tsv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49118619/

相关文章:

python - 合并两个列表,同时保持顺序并删除重复项

r - 如何在 R 中并排显示列表 - 列表的 "cbind"?

c# - 在字符串中拆分\"

python - 如何让 np.where 接受更多参数,以便它过滤 >、< 和 =;不仅仅是 > 和 <?

python - 如何根据两列的差异将行添加到数据框

python - 在 node-webkit 启动时运行 python 脚本 (nw.js)

python - 在python中按列表的顺序重复选择n项

r - 将列表列表转换为数据框

sql - 使用 CSV 文件中的值更新 Oracle 表

java - 使用 @ManyToMany 反序列化并持久化