我有三个列表
list1 = ["apple","red","1","fresh"]
list2 = ["mango","red","5","old"]
list3 = ["kiwi","green","3","fresh"]
现在我想创建一个制表符分隔的文本文件
FRUIT COLOR QUANTITY STATE
apple red 1 fresh
mango red 5 old
kiwi green 3 fresh
我正在使用此代码,但它输出的格式不正确...
with open ('processed_seq.txt','a') as proc_seqf:
for a,b,c in zip(list1,list2,list3):
proc_seqf.write("{}\t{}".format(a,b,c))
[kshk:~/server] % cat processed_seq.txt
apple mangored red1 5fresh old%
有什么建议吗?
更新:
我正在测试此列表中其他人回答的解决方案
list1 = ['JANET>WAS-TEB-01-INT', 'XXX-TR', 'XXX-TR(HTTPS)', 'XXX3>ASAbox', 'Allow charliega', 'ASAS>ASAbox', 'ASAS>ASAbox80', 'ASAS>ASAbox443', 'JilesGHS>ASAbox', 'XXX2>ASAbox', 'Allow XXX', 'JMP>GHS_ALL_NETWORKS', 'GHS_ALL_NETWORKS>JMP', 'ALL_NETWORKS>DNS', 'DNS>ALL_NETWORKS', 'JMP>AMBARI', 'JMP>DSA_TEB', 'JMP>JENKINS_TEB', 'JMP>GIT_TEB', 'JMP>RANGER_TEB', 'ALL>ZABBIX_SERV', 'ZABBIX_SERV>ALL', 'ASAbox>JANET', 'JMP>JANET2', 'DNS>EXTERNAL_DNS', 'ALL_NETWORKS>GUM', 'ALL_NETWORKS>DSA_DOG_7389', 'ALL_NETWORKS>DSA_KERB_TCP_464', 'ALL_NETWORKS>DSA_KERB_TCP_88', 'ALL_NETWORKS>DSA_KERB_UDP_464', 'ALL_NETWORKS>DSA_KERB_UDP_88', 'ALL_NETWORKS>DSA_LDAP_1', 'ALL_NETWORKS>DSA_LDAP_2', 'ALL_NETWORKS>DSA_NTP', 'BDA>BD', 'BDA>BD_UDP', 'BD>PIN-SQP-01', 'ING>-WAS-FTP-01', 'ING>BD_8080', 'WAS-TEB-01>PAP-RSV-02', 'JENKINS>JMP', 'CI>PROXY', 'ING>BD', 'BDN>DSA01', 'ALLOW XXX', 'GUM>PROXY', 'CI>ALL-SERVERS-GHS', 'deny rest']
list2 = ['None', '123.236.68.195/32', '123.236.68.195/32', '62.17.217.104/32', '10.132.8.23/32', '132.43.147.188/32', '132.43.147.188/32', '132.43.147.188/32', '100.184.251.148/32', '100.136.247.11/32', '62.18.120.237/32', '20.20.6.5/32', '20.20.0.0/16', '20.20.0.0/16', '20.20.1.58/32', '20.20.6.5/32', '20.20.6.5/32', '20.20.6.5/32', '20.20.6.5/32', '20.20.6.5/32', '20.20.0.0/16', '20.20.1.50/32', '123.122.0.149/32', '20.20.6.5/32', '20.20.1.58/32', '20.20.0.0/16', '20.20.0.0/16', '20.20.0.0/16', '20.20.0.0/16', '20.20.0.0/16', '20.20.0.0/16', '20.20.0.0/16', '20.20.0.0/16', '20.20.0.0/16', '20.20.2.0/24', '20.20.2.0/24', '20.20.3.0/24', '20.20.4.0/24', '20.20.4.0/24', '20.20.6.3/32', '20.20.1.38/32', '20.20.1.38/32', '20.20.4.0/24', '20.20.3.0/24', '188.29.165.228/32', '20.20.1.33/32', '20.20.1.38/32', 'None']
list3 = ['123.122.0.154/32', '123.122.0.149/32', '123.122.0.149/32', '123.122.0.149/32', 'None', '123.122.0.149/32', '123.122.0.149/32', '123.122.0.149/32', '123.122.0.149/32', '123.122.0.149/32', 'None', '20.20.0.0/16', '20.20.6.5/32', '20.20.1.58/32', '20.20.0.0/16', '20.20.3.7/32', '20.20.1.44/32', '20.20.1.38/32', '20.20.1.38/32', '20.20.3.7/32', '20.20.1.50/32', '20.20.0.0/16', 'None', 'None', '20.20.6.5/32', '20.20.1.33/32', '20.20.1.44/32', '20.20.1.44/32', '20.20.1.44/32', '20.20.1.44/32', '20.20.1.44/32', '20.20.1.44/32', '20.20.1.44/32', '20.20.1.44/32', '20.20.3.0/24', '20.20.3.0/24', '20.20.4.3/32', '20.20.6.7/32', '20.20.3.7/32', '20.20.2.28/32', '20.20.6.5/32', '20.20.6.5/32', '20.20.3.0/24', '20.20.1.44/32', 'None', '20.20.6.5/32', '20.20.0.0/16', 'None']
list4 = ['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', '22', 'None', '53', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None']
list5 = ['443', '22', '443', 'None', 'None', '22', '80', '443', '22', 'None', 'None', '22', 'None', '53', 'None', '8080', '443', '8081', '80', '6080', '10051', '10050', 'None', 'None', '53', '80', '7389', '464', '88', '464', '88', '389', '636', '123', 'None', '1024:65535', '22', '22', '8080', '443', '22', '3128', 'None', '443', 'None', '3128', '22', 'None']
list6 = ['allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'allow', 'deny']
答案 0 :(得分:1)
使用csv
模块:
import csv
csv.register_dialect('fruit', delimiter='\t', quoting=csv.QUOTE_NONE, lineterminator='\n')
with open('processed_seq.txt', 'a') as csvfile:
fieldnames = ['FRUIT', 'COLOR', 'QUANTITY', 'STATE']
list1 = ["apple","red","1","fresh"]
list2 = ["mango","red","5","old"]
list3 = ["kiwi","green","3","fresh"]
alllists = [fieldnames, list1, list2, list3]
writer = csv.writer(csvfile, dialect='fruit')
writer.writerows(alllists)
输出:
FRUIT COLOR QUANTITY STATE
apple red 1 fresh
mango red 5 old
kiwi green 3 fresh
答案 1 :(得分:0)
由于您不想将列写入文件您不需要zip
,因此您也省略了一些项目。
要更正格式,您可以使用str.format
指定每个项目后的空格数:
>>> for a,b,c,d in (list1,list2,list3):
... print("{:<10} {:<10} {:<10} {:<10}".format(a,b,c,d))
...
apple red 1 fresh
mango red 5 old
kiwi green 3 fresh
用于写入文件:
with open ('processed_seq.txt','a') as proc_seqf:
for a,b,c,d in (list1,list2,list3):
proc_seqf.write("{:<10} {:<10} {:<10} {:<10}".format(a,b,c,d))
如果您不想使用多个变量名,可以使用变量解包:
>>> for a in (list1,list2,list3):
... print("{:<10} {:<10} {:<10} {:<10}".format(*a))
...
apple red 1 fresh
mango red 5 old
kiwi green 3 fresh
或者:
>>> s="{:<10}"*4
>>> for a in (list1,list2,list3):
... print("s.format(*a))
...
apple red 1 fresh
mango red 5 old
kiwi green 3 fresh