[英]how can I remove special characters
How can I remove the special characters from the list如何从列表中删除特殊字符
[('testa.dom1.int', [], ['192.168.1.1'])]", ('testb.dom1.int', [], ['192.168.1.2'])]
what I want as result = [(testa.dom1.int, 192.168.1.1), (testb.dom1.int, 192.168.1.2)
我想要的结果 =
[(testa.dom1.int, 192.168.1.1), (testb.dom1.int, 192.168.1.2)
import os
import socket
import subprocess
import dns.resolver
import string
results_file = open("results.txt", "w")
my_resolver = dns.resolver.Resolver()
my_resolver.nameservers = ['192.168.1.2']
ip_list = []
name_list = []
strp_list = []
for ip in range(1, 5):
ip_list.append('192.168.1.' + str(ip))
with open(os.devnull, "wb") as limbo:
for ip in ip_list:
response = subprocess.Popen(["ping", "-c", "1", "-n", "-W", "1", ip],
stdout=limbo, stderr=limbo).wait()
if response:
print(ip + ' down')
else:
name = socket.gethostbyaddr(ip)
name_list.append(name)
**strp = str(name_list).replace("[],", '')** # is removing the []
strp_list.append(strp)
name_list = strp_list.copy()
print("3 list:", name_list)
str(name_list).replace("['", '')
print("4 list:", name_list)
print("5 list:", strp_list)
name_list = strp_list.copy()
print("6 list:", name_list)
results_file.close()
Thank you in advance先感谢您
If you want to display the contents of a list with some kind of special formatting, do not try to "fix" the natural string representation of the list.如果您想以某种特殊格式显示列表的内容,请不要尝试“修复”列表的自然字符串表示。 Instead, write your own code to build the string, inserting data from the list in the appropriate places.
相反,编写您自己的代码来构建字符串,将列表中的数据插入到适当的位置。 You can use string formatting to make this easier.
您可以使用字符串格式来简化此操作。
I assume you have the following actual data structure:我假设您具有以下实际数据结构:
my_data = [
('testa.dom1.int', [], ['192.168.1.1']),
('testb.dom1.int', [], ['192.168.1.2'])
]
and you want to create the exact string:并且您想创建确切的字符串:
goal = "[(testa.dom1.int, 192.168.1.1), (testb.dom1.int, 192.168.1.2)]"
First, let's make a function that can get the right string for each element of my_data
, and then we can put them together.首先,让我们制作一个 function 可以为
my_data
的每个元素获取正确的字符串,然后我们可以将它们放在一起。 So we want to turn eg ('testa.dom1.int', [], ['192.168.1.1'])
into "(testa.dom1.int, 192.168.1.1)"
.所以我们想把 eg
('testa.dom1.int', [], ['192.168.1.1'])
变成"(testa.dom1.int, 192.168.1.1)"
。 We'll receive one argument, which is a tuple;我们将收到一个参数,它是一个元组; to create the string, we'll unpack the tuple into separate variables, then pull the IP address string out of the nested list, then use string formatting to build the complete string.
为了创建字符串,我们将元组解包到单独的变量中,然后将 IP 地址字符串拉出嵌套列表,然后使用字符串格式化来构建完整的字符串。 That looks like:
看起来像:
def process_site(entry):
domain_name, empty_list, ip_list = entry
ip = ip_list[0]
return f'({domain_name}, {ip})'
Now we can iterate over my_data
to produce the corresponding text for each element, and use the join
method of a ', '
string to put them together;现在我们可以遍历
my_data
为每个元素生成相应的文本,并使用', '
字符串的join
方法将它们放在一起; and finally put the []
around the outside using string formatting again.最后再次使用字符串格式将
[]
放在外面。 So:所以:
contents = [process_site(entry) for entry in my_data]
joined = ', '.join(contents)
goal = f'[{joined}]'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.