如何从 python 数组的导入列表中删除标点符号
How do i remove Punctuation from import list for a python array
enter image description here我正在尝试将数据从文本文件导入到 运行 我代码中的一个测试,它正在运行,但是当我导入它时,我在它周围加上了标点符号,所以我的列表只是不同行的文本,列表中根本没有标点符号
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")
提前谢谢你,我已经附上了文本文件的图片
您的问题是您尝试过滤输入文件的方式。
因为您的 iplist.txt
文件具有以下内容:
ifmc-repserver
192.168.61.25
192.168.1.40
按如下方式更改您的代码:
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)
这将创建并打印可用于构建命令的字符串列表。
['ifmc-repserver', '192.168.61.25', '192.168.1.40']
enter image description here我正在尝试将数据从文本文件导入到 运行 我代码中的一个测试,它正在运行,但是当我导入它时,我在它周围加上了标点符号,所以我的列表只是不同行的文本,列表中根本没有标点符号
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")
提前谢谢你,我已经附上了文本文件的图片
您的问题是您尝试过滤输入文件的方式。
因为您的 iplist.txt
文件具有以下内容:
ifmc-repserver 192.168.61.25 192.168.1.40
按如下方式更改您的代码:
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)
这将创建并打印可用于构建命令的字符串列表。
['ifmc-repserver', '192.168.61.25', '192.168.1.40']