无法使用openpyxl将re.findall的值粘贴到单元格中

时间:2019-09-01 05:31:08

标签: python-3.x openpyxl

我无法在Excel单元格中打印re.findall的结果,这是从具有多行数据的文本文件中捕获的另一个re.findall的结果。

下面是整个代码中我遇到问题的部分。


import openpyxl,os,re       # -- Import

pyro_asr920_dir = '{}\\'.format(os.getcwd())        # -- MARK DIRECTORIES

input_asr920_robot_pid_wb = openpyxl.load_workbook('Data_Workbook.xlsm', keep_vba = True)     # -- OPEN EXCEL
input_asr920_robot_pid_tpd_sheet = input_asr920_robot_pid_wb['Topology-Ports_Details']

wduplinka = open(os.path.join(pyro_asr920_dir, 'DELSNACAG01C7606-logs.txt'),'r')       # -- OPEN MULTILINE TEXT FILE
uplinkacontent = wduplinka.read()

PreBBa = re.findall( r'{}[\s\S]*?!\ni'.format('interface TenGigabitEthernet4/2'), uplinkacontent)       # -- GET REQUIRED SUBSTRING WITH MATCH CRITERIA IN BETWEEN

print(PreBBa)
output01 = '''
['interface TenGigabitEthernet4/2\n isis metric 10 level-2\n!\ni']'''

for line in PreBBa:     # - 01 > I CAN PRINT THIS ON EXCEL CELL
    input_asr920_robot_pid_tpd_sheet['H27'] = line[:-1]
    print(line[:-1])
    print('-----------')
output02 = '''
interface TenGigabitEthernet4/2
 isis metric 10 level-2
!'''
# ----------------------------------------------------------------- UNABLE TO GET VALUES IN CELL

for line in PreBBa:     # - 02 > I CAN'T PRINT THIS ON EXCEL CELL {THIS IS WHERE I AM STUCK}
    if 'ospf' in line:
        theOSPF = re.findall(r'{}[\s\S]*?{}'.format(' ip ospf','\n c'), line)
        input_asr920_robot_pid_tpd_sheet['C47'] = 'Yes'
    else:
        input_asr920_robot_pid_tpd_sheet['C47'] = 'No'          # UNABLE TO GETRESULT IN EXCEL
        output03 = '''No'''
# -----------------------------------------------------------------
    metric = re.findall(r'{}[\s\S]*?{}'.format('metric ',' '), str(line))
    metric = re.findall(r'\d+',str(metric))
    input_asr920_robot_pid_tpd_sheet['C46'].value = metric[0]   # UNABLE TO GETRESULT IN EXCEL

    print(metric)
    output04 = '''10'''
# -----------------------------------------------------------------

input_asr920_robot_pid_wb.save('Data_Workbook.xlsm')

wduplinka.close()
input_asr920_robot_pid_wb.close()
print('TEST COMPLETED')

文本文件的某些内容如下:


DELSNACAG01C7606#sh running-config 
Load for five secs: 18%/1%; one minute: 26%; five minutes: 26%
Time source is NTP, 13:43:23.718 IST Fri Aug 16 2019

Building configuration...

Current configuration : 228452 bytes
!
! Last configuration change at 21:15:56 IST Thu Aug 15 2019
! NVRAM config last updated at 06:42:52 IST Sat Aug 10 2019 by cor382499
!
interface TenGigabitEthernet4/2
 isis metric 10 level-2
!
interface TenGigabitEthernet4/3
!
end

0 个答案:

没有答案