0

My ip address, country, and count output shows in random vertical. This is my print code:

print "Source IP\t\t\tCountry\t\t\tCode\t\t\tCount"
    for ip, count in non_base_10_ip:
        print "{0}\t\t\t\t\t\t\t\t\t{1}\t\t\t\t\t\t\t\t\t{2}\t\t\t\t\t\t\t\t\t{3}".format(ip,gi.country_name_by_addr(ip),gi.country_code_by_addr(ip), count)+ "\r"
    for base10ip,count in base_10_ip:
        print "{0}\t\t\t\t\t{1}\t\t\t\t\t{2}\t\t\t\t\t{3}".format(base10ip,gi.country_name_by_addr(base10ip),gi.country_code_by_addr(base10ip), count)+ "\r"

but my output shows like this:

115.237.36.185                                  China                                   CN                                  2
185.35.62.206                                   Switzerland                                 CH                                  2
74.82.47.34                                 United States                                   US                                  2
114.149.150.237                                 Japan                                   
Angeline
  • 109
  • 10
  • 2
    why not `'{0:<40}{1:<40}{3}'.format(...)` – AChampion Sep 12 '17 at 14:31
  • @AChampion probably because he doesn't know how string formatting works. – Markus Meskanen Sep 12 '17 at 14:34
  • @AChampion yes because there are 2000 data, and all of them are random – Angeline Sep 12 '17 at 14:54
  • @Angeline, using tabs instead of a fixed width (e.g. `40`) will never produce consist output for variable length strings. `{0:<40}` will force a fixed width of `40`. BTW: if you are working with lots of tablular data, `pandas` is a good library for manipulating that. – AChampion Sep 12 '17 at 14:56

0 Answers0