0

The following is the file i want to work on


slot

              slot : acu:1/1                         planned-type : ngfc-f                           actual-type : ngfc-f                           oper-status : enabled              
      error-status : no-error                                                                       availability : available            
     alarm-profile : none                                                                          capab-profile : not_applicable       
      manufacturer : ALCL                                                                                                                                                              
          mnemonic : NGFC-F                                                                                                                                                            
          pba-code :                                                                                                                                                                   
         fpba-code : 3FE64993AABA                                                                                                                                                      
          fpba-ics : 01                                                                                                                                                                
         clei-code : VAUCAJ66AA                                                                                                                                                        
         serial-no : 1743A13DC                                                                                                                                                         
       failed-test : 00:00:00:00                                                                                                                                                       
   lt-restart-time : 1970-01-01:05:45:00         lt-restart-cause : timezone_modified             lt-restart-num : 0                         restart-cnt-per-lt : 0                    

mgnt-entity-oamipaddr : 0.0.0.0 mgnt-entity-pairnum : 0 dual-host-ip : 0.0.0.0 dual-host-loc : none


slot

              slot : acu:1/1                         planned-type : ngfc-f                           actual-type : ngfc-f                           oper-status : enabled              
      error-status : no-error                                                                       availability : available            
     alarm-profile : none                                                                          capab-profile : not_applicable       
      manufacturer : ALCL                                                                                                                                                              
          mnemonic : NGFC-F                                                                                                                                                            
          pba-code :                                                                                                                                                                   
         fpba-code : 3FE64993AABA                                                                                                                                                      
          fpba-ics : 01                                                                                                                                                                
         clei-code : VAUCAJ66AA                                                                                                                                                        
         serial-no : 1743A13DC                                                                                                                                                         
       failed-test : 00:00:00:00                                                                                                                                                       
   lt-restart-time : 1970-01-01:05:45:00         lt-restart-cause : timezone_modified             lt-restart-num : 0                         restart-cnt-per-lt : 0                    

mgnt-entity-oamipaddr : 0.0.0.0 mgnt-entity-pairnum : 0 dual-host-ip : 0.0.0.0 dual-host-loc : none

I want to iterate through the text file and need an output as below in CSV

Slot    error-status alarm-profile manufacturer mnemonic
acu:1/1 no-error       none         ALCL          NGFC-F
fopen=open("gthamelslot.txt")
for lines in fopen:
    a = lines.strip()
    b=a.split()
    print(b[0][0])

I am stuck on this. Please help me with a logic with what can be done

2 Answers 2

1

You can try the following.

import re
from collections import defaultdict
import pandas as pd

result = defaultdict(list)

with open('gthamelslot.txt') as f:
    for line in f:
        match = re.search(r"\s+(\S+)\s+:\s+(\S+)", line)
        if match:
            k,v = match.group(1), match.group(2)
            if k in ["slot", "error-status", "alarm-profile", "manufacturer", "mnemonic"]:
                result[k].append(v)

df = pd.DataFrame(result)
print(df)

Update

import re
from collections import defaultdict
import pandas as pd

result = defaultdict(list)

with open('gthamelslot.txt') as f:
    for line in f:
        match = re.findall(r"\s+(\S+)\s+:\s+(\S+)", line)
        for k,v in match:
            if k in ["slot", "error-status", "alarm-profile", "manufacturer", "mnemonic", "planned-type"]:
                result[k].append(v)

df = pd.DataFrame(result)
print(df)
Sign up to request clarification or add additional context in comments.

8 Comments

It worked great but the same given texts are repeated multiple times in the text file and it is only looping through the first part. And it is also omitting the "planned-type : ngfc-f " section.
were you able to fix the problem?
Yes, but the omition is seen as explained above
You need to add planned-type to the if condition
if k in ["slot", "error-status", "alarm-profile", "manufacturer", "mnemonic", "planned-type"]:
|
0

Modify accordingly

   import csv
   filepointer = open("file.txt", 'r+')
   file = filepointer.readlines()
   filelinelist = []
   for i in file:
     filelinelist.append(i)
   firstlist = []
   secondlist = []
   for line in filelinelist:
     lineArray = line.split(":")
     first = lineArray[0].strip()
     second = lineArray[1].strip()
     firstlist.append(first)
     secondlist.append(second)
     finalList = []
     finalList.append(firstlist)
     finalList.append(secondlist)

   myFile = open('csvfile.csv', 'w')
   with myFile:
     writer = csv.writer(myFile)
     writer.writerows(finalList)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.