简体   繁体   中英

I am having Problem in output of reguler expression result is( 'IP-Subnet/mask',) but i want( IP-Subnet/mask) in out put

Below is my code i am reading.txt file then making a list and then saving it to excel but in excel i am getting ('ip subnet/mask', ) but i want only (ip subnet/mask) in out put Below are my code blocks 1.I read routing Table output from Txt file and create a list 2.then from 10.0.0.0/8 address space i remove routing table sybnets 3.I save the available IP,s to Available.txt file 4.Create List from Available.txt file 5.then i create excel file and then i save the list out put to excel in specific 10.xxx/16 sheet

import os
    import re
    import xlsxwriter
    from netaddr import *
    from openpyxl import load_workbook
    
    
    def ip_adresses():
        lst = []
        for line in fstring:
            for word in line.split():
                result = pattern.search(word)
                if result:
                    lst.append(word)
        return lst
    
    
    def write_excel(aaa, bbb, num):
        bbb = sorted(bbb)
        work11 = load_workbook(r'C:\Users\irfan\PycharmProjects\pythonProject\irfan4.xlsx')
        sheet11 = work11[aaa]
        count = sheet11.max_row
        max1 = sheet11.max_row
        for row1, entry in enumerate(bbb, start=1):
            sheet11.cell(row=row1 + max1, column=1, value=entry)
    
        work11.save("irfan4.xlsx")
    
    
    os.chdir(r'C:\Users\irfan\PycharmProjects\pythonProject')
    file = open('RR-ROUTING TABLE.txt')
    fstring = file.readlines()
    # declaring the regex pattern for IP addresses
    pattern = re.compile(r'(10\.\d{1,3}\.\d{1,3}\.\d{1,3}[/])')
    # initializing the list object
    
    unique = []
    # extracting the IP addresses
    IPs = ip_adresses()
    unique = list(dict.fromkeys(IPs))
    ipv4_addr_space = IPSet(['10.0.0.0/8'])
    ip_list = IPSet(list(unique))
    print(ip_list)
    
    available = ipv4_addr_space ^ ip_list
    print()
    
    f = open("Available.txt", "a")
    f.write(str(available))
    f.close
    print(available)
    
    workbook = xlsxwriter.Workbook('irfan4.xlsx')
    worksheet = workbook.add_worksheet()
    for row_num, data in enumerate(available):
        worksheet.write(row_num, 0, data)
    num = 0
    while num <= 255:
        worksheet = workbook.add_worksheet("10." + str(num) + ".0.0")
        num += 1
    workbook.close()
    
    # CREATE AUDIT BOOK
    ##################################################
    os.chdir(r'C:\Users\irfan\PycharmProjects\pythonProject')
    file_2 = open('Available.txt')
    fstring_2 = file_2.readlines()
    
    def ip_adresses1():
        lst = []
        for line in fstring_2:
            for word in line.split():
                result = pattern.search(word)
                if result:
                    lst.append(word)
        return lst
    List_A=ip_adresses1()
    
    print(List_A[1])
    get_list = []
    num = 0
    while num <= 255:
        pattern_sheet = re.compile(r'(10\.' + str(num) + '\.\d{1,3}\.\d{1,3}[/])')
        for get_ips in fstring_2:
            result_ip = pattern_sheet.search(get_ips)
            if result_ip:
                get_list.append(get_ips)
        sheet_name = ("10." + str(num) + ".0.0")
        write_excel(sheet_name, get_list, num)
        get_list = []
        num += 1
        enter code here

I have used re.sub function to remove characters from string

def ip_adresses1(): lst = [] for line in fstring_2: for word in line.split(): word = re.sub("IPSet", " ", word) word = re.sub(",", " ", word) word = re.sub("'", " ", word) word = re.sub("(", " ", word) word = re.sub(")", " ", word) word = re.sub("]", " ", word) word = re.sub("[", " ", word) result = pattern.search(word) if result: lst.append(word) return lst

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM