Home > Back-end >  How do i remove Punctuation from import list for a python array
How do i remove Punctuation from import list for a python array

Time:05-03

enter image description hereI am trying to import data from a text file to run a test in my code, it is working but when I import it I am getting Punctuation around it, so my list just texts on different lines and there is no Punctuation in the list at all

ip_list = []
fileob = open('iplist.txt', "r")
lines = fileob.readlines()
for line in lines:
    stripped_line = line.strip()
    line_list = stripped_line.split()
    ip_list.append(line_list)
    print(ip_list)

# ip_list = ['ifmc-repserver', '192.168.61.25', ' 192.168.1.40']
for ip in ip_list:
    response = os.popen(f"ping {ip}").read()
    if "Received = 4" in response:
        print(f"UP {ip} Ping Successful")
    else:
        import tg_start

        # print("THis is a message:"   " "   ip) #used for testing
        msg = ip
        tg_start.send_message(
            f"It appears that  {ip}"   " "   "is not responding please check the server"   " "   "om"   " "   date   " "   "at"   " "   time)
        # print(f"It appears that  {ip}"   " "   "is not responding please check the server")

Thank you in advance , i have included an image of the text file

CodePudding user response:

Your issue is how you're trying to filter your input file.

As your iplist.txt file has the following contents:

ifmc-repserver
192.168.61.25
192.168.1.40

Changing your code as follows:

ip_list = []
fileob = open('iplist.txt', "r")
lines = fileob.readlines()
for line in lines:
    stripped_line = line.strip()
    ip_list.append(stripped_line)

print(ip_list)

This will create & print a list of strings which you can use to build your commands. ['ifmc-repserver', '192.168.61.25', '192.168.1.40']

  • Related