我有这样的列表清单:
小例子:
[['chr1', '+', '14409', '1440910', 'DDX11L1\n', 'DDX11L1\n_part0'], ['chr1', '+', '14409', '1440910', 'DDX11L1\n', 'DDX11L1\n_part1']]
我想将其导入到制表符分隔的文本文件中。但是问题是\n
和5th
元素中的6th
。实际上,这些列中的\ n不是必需的。这是我使用的代码:
with open("out.csv","w") as f:
wr = csv.writer(f)
wr.writerows(listoflists)
但是文件看起来像这样:
chr1,+,14409,1440910,"DDX11L1
","DDX11L1
_part0"
chr1,+,14409,1440910,"DDX11L1
","DDX11L1
_part1"
我希望所有列表都在一行中。你知道如何解决这个问题吗?
答案 0 :(得分:1)
您已经确定了问题所在,现在只需要四处搜寻即可找到答案
for i in listoflists:
print([s.replace('\n', '') for s in i])
然后继续打印代码
with open("out.csv","w") as f:
wr = csv.writer(f)
wr.writerows(listoflists)
输出:
chr1,+,14409,1440910,"DDX11L1","DDX11L1_part0"
chr1,+,14409,1440910,"DDX11L1","DDX11L1_part1"