Python Forum
BeautifulTable print header multi times
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BeautifulTable print header multi times
#1
hello all im trying to code a tool to check the status for all websites in server IP : code :
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import requests as qan
import time 
from beautifultable import BeautifulTable
import warnings


warnings.filterwarnings('ignore', message='Unverified HTTPS request')
table = BeautifulTable()

def readfile():
    
    with open('/root/domain.txt') as fp:
        
        for line in fp:
            http = "http://" + line.rstrip()
            u = qan.get(http ,verify= False)
            u = (str(u.status_code))
            table.column_headers =["Website", "Status", "Time"]
            table.column_alignments['Website'] = BeautifulTable.ALIGN_LEFT
            table.set_style(BeautifulTable.STYLE_MARKDOWN)
            
            table.append_row([http, u, str(time.clock()) ])
            
            print(table)
            #print(http + " \t\t\t== > Status : " + u + " \t took : " + str(time.clock()) )
readfile()
when i runt it i got this result :
[Image: Screenshot-from-2019-10-28-01-40-38.png]
its print the table header multi times i need to print it just one time how i can do that ??
Reply
#2
(Oct-27-2019, 11:54 AM)evilcode1 Wrote:
def readfile():
    
    with open('/root/domain.txt') as fp:
        
        for line in fp:
            http = "http://" + line.rstrip()
            u = qan.get(http ,verify= False)
            u = (str(u.status_code))
            table.column_headers =["Website", "Status", "Time"]
its print the table header multi times i need to print it just one time how i can do that ??

Hi!

It's just a thought, but it seems that you have put the table header inside the loop, so every time it reads a line in fp, it would print the header. Have you tried leaving the header line out of the loop and at the same level of indentation as for line in fp?:

def readfile():
    
    with open('/root/domain.txt') as fp:

        table.column_headers =["Website", "Status", "Time"]        
        for line in fp:
            http = "http://" + line.rstrip()
            u = qan.get(http ,verify= False)
            u = (str(u.status_code))
All the best,



I HAVE JUST MADE A DUMMY TEST, WITH SOME MODIFICATIONS OF YOUR CODE TO BE ABLE TO RUN IT ON MY COMPUTER (SO I JUST COMMENTED OUT SOME LINES OR MADE SOME OTHER ADJUSTMENTS, BUT THAT CAN BE EASILY REVERTED AND APPLIED TO YOUR PROGRAM):

import sys
##reload(sys)
##sys.setdefaultencoding('utf8')
import requests as qan
import time 
from beautifultable import BeautifulTable
import warnings
  
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
table = BeautifulTable()
 
def readfile():
     
    with open('C:/sites_list_01.txt') as fp:

        table.column_headers =["Website", "Status", "Time"]         
        table.column_alignments['Website'] = BeautifulTable.ALIGN_LEFT
        table.set_style(BeautifulTable.STYLE_MARKDOWN)
        for line in fp:     
            table.append_row(['http://XXXXXXXXX', '200', str(time.clock()) ])
##            http = "http://" + line.rstrip()
##            u = qan.get(http ,verify= False)
##            u = (str(u.status_code))   
##            print(http + " \t\t\t== > Status : " + u + " \t took : " + str(time.clock()) )
        print(table)
readfile()
where 'C:/sites_list_01.txt' is a dummy text file with just 11 sites that I used to test the program (I have already eliminated it), but the program with these modifications to run on my computer produced the following output (just one header row):

Output:
| Website | Status | Time | |------------------|--------|-------| | http://XXXXXXXXX | 200 | 0.632 | | http://XXXXXXXXX | 200 | 0.632 | | http://XXXXXXXXX | 200 | 0.632 | | http://XXXXXXXXX | 200 | 0.632 | | http://XXXXXXXXX | 200 | 0.632 | | http://XXXXXXXXX | 200 | 0.632 | | http://XXXXXXXXX | 200 | 0.632 | | http://XXXXXXXXX | 200 | 0.632 | | http://XXXXXXXXX | 200 | 0.632 | | http://XXXXXXXXX | 200 | 0.632 | | http://XXXXXXXXX | 200 | 0.632 | >>>
So I think you could adapt it to your desired output.

All the best,



I think I should also point out that I received a deprecation warning message:

Error:
DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead.
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#3
thank u brother for replay and for trying to help but unfortunately does not work i test it on multi machines with py3 and py2 i dont know what is the problem :( i will send u my domain list on inbox check it and try to help me : my final code :

# encoding=utf8
import sys
#reload(sys)
#sys.setdefaultencoding('utf8')
import requests as qan
import time 
from beautifultable import BeautifulTable
import warnings
from datetime import datetime
now = datetime.now() # current date and time
from timeit import default_timer as timer


warnings.filterwarnings('ignore', message='Unverified HTTPS request')

table = BeautifulTable()

def readfile():
    
    with open('/root/domain.txt') as fp:
        
        for line in fp:
            try:
                start = timer()
                http = "http://" + line.rstrip()
                u = qan.get(http ,verify= False)
                u = (str(u.status_code))
                table.column_headers =["Website", "Status", "Time S/M "]
                table.column_alignments['Website'] = BeautifulTable.ALIGN_LEFT
                table.set_style(BeautifulTable.STYLE_MARKDOWN)
                end = timer()
                table.append_row([http, u, str(end - start) ])
   
                print(table)
                #print(http + " \t\t\t== > Status : " + u + " \t took : " + str(time.clock()) )
                continue
            except qan.exceptions.HTTPError as errh:
                continue
            except qan.exceptions.ConnectionError as errc:
                continue
            except qan.exceptions.Timeout as errt:
                continue
            except qan.exceptions.RequestException as err:
                continue



readfile()
Output:
| Website | Status | Time S/M | |---------------------|--------|-----------| | http://xxxxx.com.ni | 200 | 1.008 | | Website | Status | Time S/M | |--------------------------------|--------|-----------| | http://xxxxx.com.ni | 200 | 1.008 | | http://xxxxxxxxxxxxxxxx.com.ni | 200 | 0.82 | | Website | Status | Time S/M | |--------------------------------|--------|-----------| | http://xxxxx.com.ni | 200 | 1.008 | | http://xxxxxxxxxxxxxxxx.com.ni | 200 | 0.82 | | http://xxxxxxx.com.ni | 200 | 0.516 | | Website | Status | Time S/M | |--------------------------------|--------|-----------| | http://xxxxx.com.ni | 200 | 1.008 | | http://xxxxxxxxxxxxxxxx.com.ni | 200 | 0.82 | | http://xxxxxxx.com.ni | 200 | 0.516 | | http://xxxxxxxxx.com.ni | 200 | 0.538 |
Reply
#4
Hi again!

My understanding, seeing your first post, is that your code runs properly, but it prints the header row every time it reads for line in fp because you made the program to print the headers inside the loop for every line read in fp with this part of your code:

def readfile():
     
    with open('/root/domain.txt') as fp:
         
        for line in fp:
            http = "http://" + line.rstrip()
            u = qan.get(http ,verify= False)
            u = (str(u.status_code))
            table.column_headers =["Website", "Status", "Time"]
            table.column_alignments['Website'] = BeautifulTable.ALIGN_LEFT
            table.set_style(BeautifulTable.STYLE_MARKDOWN)
             
            table.append_row([http, u, str(time.clock()) ])
I think that if the image you posted corresponds to that first program, then you should try to leave out of the loop, just the part establishing the unchangeable characteristics to set the table.

If the program that you posted on your first post was running fine, and the only thing not wanted was that the header line was printed more than once, then I would suggest you try this one:

# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import requests as qan
import time 
from beautifultable import BeautifulTable
import warnings
 
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
table = BeautifulTable()
 
def readfile():     
    with open('/root/domain.txt') as fp:
        table.column_headers =["Website", "Status", "Time"]         
        table.column_alignments['Website'] = BeautifulTable.ALIGN_LEFT
        table.set_style(BeautifulTable.STYLE_MARKDOWN)         
        for line in fp:
            http = "http://" + line.rstrip()
            u = qan.get(http ,verify= False)
            u = (str(u.status_code))             
            table.append_row([http, u, str(time.clock()) ])             
            print(table)
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#5
the same problem brother :( sorry
[Image: Screenshot-from-2019-10-30-01-14-10.png]
Reply
#6
I think line 23 must be outside the loop. At the moment you print the table in every iteration, just adding one more line.
newbieAuggie2019 showed it in his first post already
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Oct-29-2019, 11:20 AM)buran Wrote: I think line 23 must be outside the loop. At the moment you print the table in every iteration, just adding one more line
Oops! I think Buran is right, otherwise a table would be printed again, with the header row again. Sorry for not seeing it myself.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#8
(Oct-29-2019, 11:23 AM)newbieAuggie2019 Wrote: Sorry for not seeing it myself.
Well, as I said - you did it in your first post - your line 25 is outside the loop body :-) They didn't implement it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
which line ??
table.column_headers =["Website", "Status", "Time"] 
Reply
#10
the line you refer is already outside the loop (line 15 in your latests code)
(Oct-29-2019, 11:23 AM)buran Wrote: line 23 must be outside the loop.
that is print(table)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to print string multiple times on new line ace19887 7 5,779 Sep-30-2020, 02:53 PM
Last Post: buran
  How to print string multiple times separated by delimiter Mekala 1 1,913 May-23-2020, 09:21 AM
Last Post: Yoriz
  Print 'X' a number of times Than999 1 2,661 Jan-18-2020, 06:41 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020