简体   繁体   English

Python-将元组列表水平写入文本文件

[英]Python - Write list of tuples horizontally to a text file

I have a list of tuples as follows: 我有一个元组列表,如下所示:

listo = [ (A,1),(B,2),(C,3) ]

I want to write this list to a file as below: 我想将此列表写入文件,如下所示:

A   B   C
1   2   3

I tried the following and it gave the output as below: 我尝试了以下操作,它的输出如下:

with open('outout.txt', 'w') as f:
    for x, y in listo:
        f.write("{0}\t{1}\n".format(x,y)

A   1
B   2
C   3

I tried switching up the \\t and \\n in f.write function and also played with the format function. 我尝试在f.write函数中切换\\ t和\\ n,并且还使用了format函数。 Nothing worked. 没事。

What did I miss here? 我在这里想念什么?

The csv module can certainly help you out here: csv模块当然可以在这里为您提供帮助:

First, separate out the headers and the values with a call to zip . 首先,通过调用zip分隔标题和值。 Then write them out to your file with csv 然后使用csv将它们写到您的文件中

In [15]: listo
Out[15]: [('A', 1), ('B', 2), ('C', 3)]

In [16]: headers, vals = zip(*listo)

In [17]: headers
Out[17]: ('A', 'B', 'C')

In [18]: vals
Out[18]: (1, 2, 3)

The full solution: 完整的解决方案:

import csv

listo = [(A,1), (B,2), (C,3)]
headers, vals = zip(*listo)

with open('output.txt', 'w') as outfile:
    writer = csv.writer(outfile, delimiter='\t')
    writer.writerow(headers)
    writer.writerow(vals)

One of the ways is to seperate the two elements in each tuple into two different lists (or tuples) 一种方法是将每个元组中的两个元素分成两个不同的列表(或元组)

with open('outout.txt', 'w') as f:
    for x, y in listo:
        f.write("{}\t".format(x))
    f.write("\n")
    for x, y in listo:
        f.write("{}\t".format(y))

Or you can use join 或者您可以使用join

a = "\t".join(i[0] for i in listo)
b = "\t".join(i[1] for i in listo)
with open('outout.txt', 'w') as f:
    f.write("{}\n{}".format(a,b))

You need to transpose/unzip the list first. 您需要先对列表进行转置/解压缩。 This is done with the idiom zip(*list_) . 这是通过成语zip(*list_)

# For Python 2.6+ (thanks iCodez):
# from __future__ import print_function

listo = [("A", 1), ("B", 2), ("C", 3)]
transposed = zip(*listo)
letters, numbers = transposed

with open("output.txt", "w") as output_txt:
    print(*letters, sep="\t", file=output_txt)
    print(*numbers, sep="\t", file=output_txt)

File output.txt : 文件output.txt

A   B   C
1   2   3

Just try doing separate loops: 只需尝试执行单独的循环:

with open('outout.txt', 'w') as f:
for x in listo:
    f.write('{}\t'.format(x[0])) # print first element with tabs
f.write('\n') # print a new line when finished with first elements
for y in listo:
    f.write('{}\t'.format(x[1])) # print second element with tabs
f.write('\n') # print another line
>>> A = 'A'
>>> B = 'B'
>>> C = 'C'
>>> listo = [ (A,1),(B,2),(C,3) ]
>>> print(*zip(*listo))
('A', 'B', 'C') (1, 2, 3)
>>> print(*('\t'.join(map(str, item)) for item in zip(*listo)), sep='\n')
A       B       C
1       2       3
>>> with open('outout.txt', 'w') as f:
...     for item in zip(*listo):
...         f.write('\t'.join(map(str, item)) + '\n')
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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