简体   繁体   English

如何删除特殊字符

[英]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.

相关问题 如何删除特殊字符 - How to remove special characters 如何在一次传递中预处理NLP文本(小写,删除特殊字符,删除数字,删除电子邮件等)? - How can I preprocess NLP text (lowercase, remove special characters, remove numbers, remove emails, etc) in one pass? 如何仅删除数据框中一列的特殊字符? - How can I remove special characters for just one column in a data frame? 如何修改和删除Python2字典键中的特殊字符 - How can I modify and remove special characters in keys of Python2 dictionary 如何在不使用任何外部库的情况下从字符串中删除特殊字符? - How can I remove special characters from a string without using any external library? 如何在python中删除特殊字符? - How to remove special characters in python? 如何从嵌套字典中的值中删除特殊字符 - How do I remove special characters from the values in a nested dictionary 如何强制sklearn CountVectorizer不删除特殊字符(即#,@ 、、 $或%) - How to force sklearn CountVectorizer to not remove special characters (i.e. #, @, , $ or %) 如何删除 output 打印中的特殊字符(方括号、(') 和逗号) - How do i remove special characters in output print (brackets, the (') and the comma) 如何转义Python字符串中的任何特殊shell字符? - How can I escape any of the special shell characters in a Python string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM