How to convert the text format exported by the command to a regular csv file

problem description

there is a command line parameter exported from the network management, which needs to be normalized to a csv file. Try to read it with python, but I don"t know how to do it after reading it. Can anyone help me? thank you.

the contents of the quoted text file are as follows:
Script Task : LST 2CMD

MML Command-----DSP CELLPHYTOPO:;
NE : ZRT0600X_2NB01
Report : PP+    ZRT0600X_2NB01        2018-09-19 17:34:35
O&M    -sharp806395846
%%/*1880518684*/DSP CELLPHYTOPO:;%%
RETCODE = 0  Operation succeeded.

Display Physical Topology of Cells
----------------------------------
zNodeB ID  Local Cell ID  ZF Module Information  Connected ZBP Port Information  Served ZBP Information  Served Zbp Primary Secondary Mode

6965       0              0-60-0                 0-0-3-0,0-0-2-0                 0-0-2                   PRIMARY                          
6965       1              0-61-0                 0-0-3-1,0-0-2-1                 0-0-2                   PRIMARY                          
6965       2              0-62-0                 0-0-3-2,0-0-2-2                 0-0-2                   PRIMARY                                                                            
(Number of results = 12)


---    END



MML Command-----DSP CELLPHYTOPO:;
NE : ZYG6738P_9NB01
Report : PP+    ZYG6738P_9NB01        2018-09-19 17:34:35
O&M    -sharp807459798
%%/*1879088941*/DSP CELLPHYTOPO:;%%
RETCODE = 0  Operation succeeded.

Display Physical Topology of Cells
----------------------------------
zNodeB ID  Local Cell ID  ZF Module Information  Connected ZBP Port Information  Served ZBP Information  Served Zbp Primary Secondary Mode

113975     60             0-90-0                 0-0-1-0                         0-0-3                   PRIMARY                          
113975     61             0-91-0                 0-0-1-1                         0-0-3                   PRIMARY                          
113975     62             0-92-0                 0-0-1-2                         0-0-3                   PRIMARY                                                   
(Number of results = 6)


---    END



MML Command-----LST CELLDLPCPDSCHPA:;
NE : ZTT7733P_9NB02
Report : PP+    ZTT7733P_9NB02        2018-09-19 17:40:35
O&M    -sharp807416869
%%/*1879234989*/LST CELLDLPCPDSCHPA:;%%
RETCODE = 0  Operation succeeded.

List Pa parameters for PDSCH power control
------------------------------------------
Local cell ID  PA distribution(dB)      Nominal PDSCH Offset(dB)

60             -3 dB                               2                                  
61             -3 dB                               2                                  
(Number of results = 2)


---    END



MML Command-----LST CELLDLPCPDSCHPA:;
NE : ZKD1056X_2NB01
Report : PP+    ZKD1056X_2NB01        2018-09-19 17:40:35
O&M    -sharp806381903
%%/*1879234853*/LST CELLDLPCPDSCHPA:;%%
RETCODE = 0  Operation succeeded.

List Pa parameters for PDSCH power control
------------------------------------------
Local cell ID  PA distribution(dB)      Nominal PDSCH Offset(dB)

0              -3 dB                               2                                  
1              -3 dB                               2                                  
2              -3 dB                               2                                                                  
(Number of results = 12)


---    END
======================================

the environmental background of the problems and what methods you have tried

wrote simple python code, only read

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

the result looks like this:

Jul.08,2021

the easiest way is regular matching


ashamed that I don't know how to write


has written a simple analysis program. You can try

-sharp!/usr/bin/env python
-sharp -*- coding: utf-8 -*-
import os
import sys
import time


def read_file(pth):
    cont = None
    try:
        with open(pth) as fp:
            cont = fp.read()
    except Exception as err:
        print(err)
    return cont


def write_file(dat, pth):
    try:
        with open(pth, 'w') as _fp:
            _fp.write(dat)
    except Exception as _err:
        print(_err)


SHARED_HEADER = ['MML Command', 'NE']
HANDLER = {
    'CELLPHYTOPO': 'DspFmt',
    'CELLDLPCPDSCHPA': 'LstFmt',
}

-sharp zNodeB ID Local Cell ID ZF Module Information Connected ZBP Port Information Served ZBP Information Served Zbp Primary Secondary Mode
DSP = SHARED_HEADER
DspFmt_keys = [1, 2, 2, 3, 2, 4]
-sharp Local cell ID PA distribution(dB) Nominal PDSCH Offset(dB)
LST = SHARED_HEADER
LstFmt_keys = [2, 1, 2]


class LogFmt(object):
    def __init__(self, raw_lines, key_fmt=None):
        if len(raw_lines) <= 9:
            raise Exception('Not Valid Log!')
        self.columns = []
        self.rows = []
        self.key_fmt = key_fmt
        self.headers = []

        self.parse(raw_lines)

    def parse(self, txt):
        self._cmd_header(txt[0:2])
        self._keys(txt[7])
        self._cells(txt[8:-1])

    def _cmd_header(self, txt):
        cmd = txt[0].split('-----')[1]
        self.headers.append(SHARED_HEADER[0])
        self.columns.append(cmd)

        ne = txt[1].split(':')[1].strip()
        self.headers.append(SHARED_HEADER[1])
        self.columns.append(ne)

    def _keys(self, txt):
        _k = gen_key(txt, self.key_fmt)
        for _ in _k:
            self.headers.append(_)

    def _cells(self, txt):
        -sharp  ',',  '|'
        txt = [x.replace(',', '|') for x in txt]
        _arr = [self.columns + x.split(' ') for x in txt]
        for ar in _arr:
            self.rows.append(ar)


class DspFmt(LogFmt):
    def __init__(self, raw_lines, fmt_key):
        super(DspFmt, self).__init__(raw_lines, fmt_key)


class LstFmt(LogFmt):
    def __init__(self, raw_lines, fmt_key):
        super(LstFmt, self).__init__(raw_lines, fmt_key)

    def _cells(self, txt):
        _arr = [x.split(' ') for x in txt]
        for ar in _arr:
            col = []
            col.append(ar[0])
            col.append(' '.join(ar[1:3]))
            col.append(ar[3])

            c = self.columns + col
            self.rows.append(c)


def gen_key(key_str, _blks):
    -sharp  _blks  key
    _l = key_str.split(' ')
    _keys = []
    i = 0
    for n in _blks:
        _k = ' '.join(_l[i: i + n + 1])
        _keys.append(_k)
        i += n + 1

    return _keys


def tocsv(dat):
    mmls = [x for x in dat.split('--- END') if x]
    dat = {}
    for mml in mmls:
        -sharp ,  \r\n
        m = [x for x in mml.split('\n') if x]
        if not len(m):
            continue

        h = m[0].split(':')[0].split(' ')[-1]
        h = HANDLER[h]
        handler_ = globals().get(h)
        mk = h + '_keys'
        _key = globals().get(mk)
        d = handler_(m, _key)
        dat.setdefault(mk, {
            'headers': d.headers,
            'rows': []
        })

        dat[mk]['rows'] += d.rows

    for _, d in dat.items():
        txt = ''
        txt += ','.join(d['headers']) + '\n'
        for row in d['rows']:
            txt += ','.join(row) + '\n'

        name_ = str(int(time.time())) + _ + '.csv'
        write_file(txt, name_)
        print('Save to {}'.format(name_))


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('log file pth needed!\n\tusage: python/python3 to_csv.py your_log_pth')
        os._exit(-1)

    dat = read_file(sys.argv[1])
    tocsv(dat)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b322df-2be08.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b322df-2be08.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?