-1

i have three lists

list1 = ["apple","red","1","fresh"]
list2 = ["mango","red","5","old"]
list3 = ["kiwi","green","3","fresh"]

now i want to create a tab-delimited text file

FRUIT   COLOR   QUANTITY   STATE
apple   red     1          fresh
mango   red     5          old
kiwi    green   3          fresh

I am using this code, however it does not output in correct format...

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%  

any suggestions?

UPDATE:

I am testing out the solutions answered by others on this list

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']
krisdigitx
  • 7,068
  • 20
  • 61
  • 97
  • 1
    *"it does not output in correct format"* - so what **does** it do? For one thing, you've got three arguments to `format` but only two placeholders.. – jonrsharpe Oct 08 '15 at 10:01

2 Answers2

1

Using the csv module:

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)

Output:

FRUIT   COLOR   QUANTITY    STATE
apple   red 1   fresh
mango   red 5   old
kiwi    green   3   fresh
Joe Young
  • 5,749
  • 3
  • 28
  • 27
  • cool, good suggestion however the output is not pretty especially the last line... – krisdigitx Oct 08 '15 at 10:44
  • What kind of output do you want exactly? Your original question asked for **tab** delimited output, not fixed-width formatting. This is tab delimited output. – Joe Young Oct 08 '15 at 10:46
  • In that case, the csv module is not for you. But I would like to point you to this discussion, just FYI: http://stackoverflow.com/a/6949967/2744166 Cheers. – Joe Young Oct 08 '15 at 10:50
0

Since you don't want to write the columns to file You don't need zip, also you have omitted some of the items.

And for correcting the format you can use str.format with specifying the number of white spaces after each item :

>>> 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 

For writing in file :

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))

If you don't want to use multiple variable names you can use variable unpacking :

>>> 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 

Or :

>>> 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 
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • thanks, that works, however how can I auto accomodate to the lenght of the list, i.e here it is only 4 items on the list, what if it was for 100 items in the list? – krisdigitx Oct 08 '15 at 10:37
  • @krisdigitx Also if it works, you can tell this to community by [accepting](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) the answer. – Mazdak Oct 08 '15 at 10:46
  • the number of items on my original list , si about 20, do i need to add 20 of these "{:<10}" ??? – krisdigitx Oct 08 '15 at 10:53
  • @krisdigitx No you can create your format string by multiplying : before the loop `>>> s="{:<10} "*4 >>> print(s.format(*a)) ` – Mazdak Oct 08 '15 at 10:56
  • see the new list above, the output using s.format is not pretty – krisdigitx Oct 08 '15 at 12:25