python中生成的文本文件格式错误 [英] wrong format of generated text file in python
问题描述
我有一个包含许多元组列表的大父列表,就像小例子一样:
<预><代码>[[('id', 'name', 'Trans'), ('ENS001', 'EGSB', 'TTP')],[('id', 'name', 'Trans'), ('EN02', 'EHGT', 'GFT')]]我的目标是制作一个文本文件,其中有一些列.列是父列表中每个列表的第二个元组.所有列表中的第一个元组都相似在所有嵌套列表中,它们将是列名.
我使用了这个代码(z在列表之上)
rows= [i[1] for i in z]
得到
[('ENS001', 'EGSB', 'TTP'), ('EN02', 'EHGT', 'GFT')]
还有这个(我称之为A")
with open('out.txt','w') as f :f.write (' '.join(z[0][0]))对于 i 在行中:f.write(' '.join(i))
获取文件.但是在文件中,列不是这样分隔的.
id 名称 TransENS001 EGSB TTPEN02 EHGT GFT
你写在一行,你需要添加一个换行符:
rows = (sub[1] for sub in z)with open('out.txt','w') as f:f.write ("{}\n".format(' '.join(z[0][0]))) # 添加换行符对于 i 在行中:f.write ("{}\n".format(' '.join(i))) # 再次换行
如果您的行中总是有三个元素并且您希望它们对齐:
rows = [sub[1] for sub in z]mx_len = 0对于 tup 行:mx = len(max(tup[:-1],key=len))如果 mx >mx_len:mx_len = mxwith open('out.txt', 'w') as f:a, b, c = z[0][0]f.write("{:<{mx_len}} {:<{mx_len}} {}\n".format(a, b, c, mx_len=mx_len))对于行中的 a、b、c:f.write("{:<{mx_len}} {:<{mx_len}} {}\n".format(a, b, c, mx_len=mx_len))
输出:
id 名称 TransENS001 EGSB TTPEN02 EHGT GFT
如果长度不同:
with open('out.txt', 'w') as f:f.write(("{:<{mx_len}}"*len(z[0][0])).format(*z[0][0], mx_len=mx_len) + "\n")对于行中的行:f.write(("{:<{mx_len}}"*len(row)).format(*row, mx_len=mx_len) + "\n")
I have a big parent list containing many lists of tuples like the small example:
[ [('id', 'name', 'Trans'), ('ENS001', 'EGSB', 'TTP')], [('id', 'name', 'Trans'), ('EN02', 'EHGT', 'GFT')] ]
My goal is to make a text file in which there would some columns. The columns are the second tuple of each list in the parent list. The first tuple in all lists are similar in all nested lists and they would be column names.
I used this code(z is above list)
rows= [i[1] for i in z]
to get
[('ENS001', 'EGSB', 'TTP'), ('EN02', 'EHGT', 'GFT')]
And this one (which I call it "A")
with open('out.txt','w') as f : f.write (' '.join(z[0][0])) for i in rows: f.write (' '.join(i))
to get the file. But in the file the columns are not separated like this.
id name Trans
ENS001 EGSB TTP
EN02 EHGT GFT
You are writing it all on one line, you need to add a newline:
rows = (sub[1] for sub in z)
with open('out.txt','w') as f:
f.write ("{}\n".format(' '.join(z[0][0]))) # add a newline
for i in rows:
f.write ("{}\n".format(' '.join(i))) # newline again
If you always have three elements in your rows and you want them aligned:
rows = [sub[1] for sub in z]
mx_len = 0
for tup in rows:
mx = len(max(tup[:-1],key=len))
if mx > mx_len:
mx_len = mx
with open('out.txt', 'w') as f:
a, b, c = z[0][0]
f.write("{:<{mx_len}} {:<{mx_len}} {}\n".format(a, b, c, mx_len=mx_len))
for a, b, c in rows:
f.write("{:<{mx_len}} {:<{mx_len}} {}\n".format(a, b, c, mx_len=mx_len))
Output:
id name Trans
ENS001 EGSB TTP
EN02 EHGT GFT
If the length varies:
with open('out.txt', 'w') as f:
f.write(("{:<{mx_len}}"*len(z[0][0])).format(*z[0][0], mx_len=mx_len) + "\n")
for row in rows:
f.write(("{:<{mx_len}}"*len(row)).format(*row, mx_len=mx_len) + "\n")
这篇关于python中生成的文本文件格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!