Can't paste value of re.findall in cell using openpyxl - python-3.x

I'm unable to print the result of re.findall in an Excel cell, which is the result of another re.findall which is captured from a text file with multi-line data.
Below is the section of code from the entire code where I am facing a problem.
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')
Some content of Text file is as below:
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

Related

How do i count number of occurence

i need to create a top 5 of interfaces that went up/down based on %LINK-3-UPDOWN from a log file.
and also need to count the amount of ICMP packets that are stopped based on amount of %SEC-6-IPACCESSLOGDP.
log file looks like this:
Sep 22 15:12:09 145.89.109.1 : %SEC-6-IPACCESSLOGP: list 120 denied tcp 80.82.77.33(0) -> 145.89.109.49(0), 1 packet
Sep 22 16:11:15 145.89.109.11 28w6d: %LINK-3-UPDOWN: Interface GigabitEthernet1/20, changed state to up
Sep 22 16:11:15 145.89.109.11 28w6d: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet1/20, changed state to up
Sep 22 15:16:09 145.89.109.1 : %SEC-6-IPACCESSLOGP: list 120 denied tcp 216.158.238.186(0) -> 145.89.109.49(0), 1 packet
Sep 22 15:17:10 145.89.109.1 : %SEC-6-IPACCESSLOGP: list 120 denied tcp 184.105.139.98(0) -> 145.89.109.49(0), 1 packet
Sep 22 15:22:10 145.89.109.1 : %SEC-6-IPACCESSLOGS: list 78 denied 145.89.110.15 1 packet
Sep 22 16:20:46 145.89.109.11 28w6d: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet1/20, changed state to down
Sep 22 16:20:46 145.89.109.11 28w6d: %LINK-3-UPDOWN: Interface GigabitEthernet1/20, changed state to down
My code is as followed but i am not getting the result i want:
infile = open("router1.log","r") #Open log bestand in "read modus"
dictionary = {} #Maak lege dictionary aan
for line in infile: #For-loop die elke regel afgaat in log-bestand
try:
naam = line.split(":")[3] #variable naam die regel split naar een lijst met index 3
naam2 = line.split(":")[4] #variable naam die regel split naar een lijst met index 4
if naam.strip()in dictionary.keys(): #"Als" naam zich bevindt in dictionary voer onderstaande uit:
dictionary[naam.strip()]+=1
else: #Anders voer onderstaan uit:
dictionary[naam.strip()]=0
except:
continue
If I had interpreted your question correctly, your problem here is that you are unable to obtain the correct count values.
To resolve that, you want set your first occurrence of a particular log issue to value 1 instead of 0.
So your else statement should be:
else:
dictionary[naam.strip()]=1
If you do not do that your counts will always be lesser by 1, hope that helps!

Python : I have text file and need to sort by ascending order date-time data

I have Python code (3.x) like this. The code is read data from the database and write into the text file (file2.text) which date are folder name & last modified date. I need to sort data of last modified date by sort by ascending.
I have tried code about sorting but it doesn't work.
`
My text file is contain this information
Foldername: last modified:
AR112 2018-06-21 11:42:45
AR231 2018-07-06 14:25:10
AR234 2018-06-28 15:10:54
AR784 2018-07-06 14:25:40
AR142 2018-04-26 10:35:59
AR147 2018-06-21 11:56:26
OTHER 2018-12-10 13:18:21
*** Last folder created by MC1 at *** :Fri Mar 15 10:41:26 2019
and I would like to get this below(date/time sorting by ascending) and write back into text file
Foldername: last modified:
AR142 2018-04-26 10:35:59
AR112 2018-06-21 11:42:45
AR147 2018-06-21 11:56:26
AR234 2018-06-28 15:10:54
AR231 2018-07-06 14:25:10
AR784 2018-07-06 14:25:40
OTHER 2018-12-10 13:18:21
*** Last folder created by MC1 at *** :Fri Mar 15 10:41:26 2019`
Here is my code.
--- Test Working
#print("Folder is : ",time.ctime(max(os.stat(r).st_mtime for r, d, _ in os.walk(thisdir))))
all_subdirs = (d for d in os.listdir(thisdir) if os.path.isdir(d))
latest_subdir = max(all_subdirs, key=os.path.getmtime, _ )
#latest_subdir = max((os.path.getmtime(f), f) for f in all_subdirs)[1]
print("XXXX=",latest_subdir)
# --- End working
Here's my solution: Read in all the lines, sort them by the date, and write them back to a file. In order not to trip over the first and last lines, I handled them seperately, but you could also remove them from the lines before.
from datetime import datetime
with open("my_file") as f:
lines = f.readlines()
def sort_key(line):
if line.startswith("Folder"):
return 0 # put this at the beginning
elif line.startswith("***"):
return float("inf") # infinity; put this at the end
date_str = line.strip().split(maxsplit=1)[1]
date = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
return date.toordinal()
lines.sort(key=sort_key)
with open("new_file", "w") as f:
for line in lines:
f.write(line)
f.write("\n")

svm train output file has less lines than that of the input file

I am currently building a binary classification model and have created an input file for svm-train (svm_input.txt). This input file has 453 lines, 4 No. features and 2 No. classes [0,1].
i.e
0 1:15.0 2:40.0 3:30.0 4:15.0
1 1:22.73 2:40.91 3:36.36 4:0.0
1 1:31.82 2:27.27 3:22.73 4:18.18
0 1:22.73 2:13.64 3:36.36 4:27.27
1 1:30.43 2:39.13 3:13.04 4:17.39 ......................
My problem is that when I count the number of lines in the output model generated by svm-train (svm_train_model.txt), this has 12 fewer lines than that of the input file. The line count here shows 450, although there are obviously also 9 lines at the beginning showing the various parameters generated
i.e.
svm_type c_svc
kernel_type rbf
gamma 1
nr_class 2
total_sv 441
rho -0.156449
label 0 1
nr_sv 228 213
SV
Therefore 12 lines in total from the original input of 453 have gone. I am new to svm and was hoping that someone could shed some light on why this might have happened?
Thanks in advance
Updated.........
I now believe that in generating the model, it has removed lines whereby the labels and all the parameters are exactly the same.
To explain............... My input is a set of miRNAs which have been classified as 1 and 0 depending on their involvement in a particular process or not (i.e 1=Yes & 0=No). The input file looks something like.......
0 1:22 2:30 3:14 4:16
1 1:26 2:15 3:17 4:25
0 1:22 2:30 3:14 4:16
Whereby, lines one and three are exactly the same and as a result will be removed from the output model. My question is then both why the output model would do this and how I can get around this (whilst using the same features)?
Whilst both SOME OF the labels and their corresponding feature values are identical within the input file, these are still different miRNAs.
NOTE: The Input file does not have a feature for miRNA name (and this would clearly show the differences in each line) however, in terms of the features used (i.e Nucleotide Percentage Content), some of the miRNAs do have exactly the same percentage content of A,U,G & C and as a result are viewed as duplicates and then removed from the output model as it obviously views them as duplicates even though they are not (hence there are less lines in the output model).
the format of the input file is:
Where:
Column 0 - label (i.e 1 or 0): 1=Yes & 0=No
Column 1 - Feature 1 = Percentage Content "A"
Column 2 - Feature 2 = Percentage Content "U"
Column 3 - Feature 3 = Percentage Content "G"
Column 4 - Feature 4 = Percentage Content "C"
The input file actually looks something like (See the very first two lines below), as they appear identical, however each line represents a different miRNA):
1 1:23 2:36 3:23 4:18
1 1:23 2:36 3:23 4:18
0 1:36 2:32 3:5 4:27
1 1:14 2:41 3:36 4:9
1 1:18 2:50 3:18 4:14
0 1:36 2:23 3:23 4:18
0 1:15 2:40 3:30 4:15
In terms of software, I am using libsvm-3.22 and python 2.7.5
Align your input file properly, is my first observation. The code for libsvm doesnt look for exactly 4 features. I identifies by the string literals you have provided separating the features from the labels. I suggest manually converting your input file to create the desired input argument.
Try the following code in python to run
Requirements - h5py, if your input is from matlab. (.mat file)
pip install h5py
import h5py
f = h5py.File('traininglabel.mat', 'r')# give label.mat file for training
variables = f.items()
labels = []
c = []
import numpy as np
for var in variables:
data = var[1]
lables = (data.value[0])
trainlabels= []
for i in lables:
trainlabels.append(str(i))
finaltrain = []
trainlabels = np.array(trainlabels)
for i in range(0,len(trainlabels)):
if trainlabels[i] == '0.0':
trainlabels[i] = '0'
if trainlabels[i] == '1.0':
trainlabels[i] = '1'
print trainlabels[i]
f = h5py.File('training_features.mat', 'r') #give features here
variables = f.items()
lables = []
file = open('traindata.txt', 'w+')
for var in variables:
data = var[1]
lables = data.value
for i in range(0,1000): #no of training samples in file features.mat
file.write(str(trainlabels[i]))
file.write(' ')
for j in range(0,49):
file.write(str(lables[j][i]))
file.write(' ')
file.write('\n')

Search multiline error log for error code and then some of it's parameters on Linux

What command would give me the output I need for each instance of an error code in a very large log file? The file has records marked by a begin and end with number of characters. Such as:
SR 120
1414760452 0 1 Fri Oct 31 13:00:52 2014 2218714 4
GROVEMR2 scn
../SrxParamIF.m 284
New Exam Started
EN 120
The 5th field is the error code, 2218714 in previous example.
I thought of just grep'ing for the error code and outputting -A lines afterwards; then picking what I needed from that rather than parsing the entire file. That seems easy but my grep/awk/sed usage isn't to that level.
ONLY when error 2274021 is encountered as in the following example I'd like some output as shown.
Show me output such as: egrep ‘Coil:|Connector:|Channels faulted:| First channel:’ ERRORLOG|less
Part of input file of interest:
Mon Nov 24 13:43:37 2014 2274021 1
AWHMRGE3T NSP
SCP:RfHubCanHWO::RfBias 4101
^MException Class: Unknown Severity: Unknown
Function: RF: RF Bias
PSD: VIBRANT Coil: Breast SMI Scan: 1106/14
Coil Fault - Short Circuit
A multicoil bias fault was detected.
.
Connector: Port 1 (P1)
Channels faulted: 0x200
First channel: 10 of 32, counting from 1
Fault value: -2499 mV, Channel: 10->
Output:
Coil: Breast SMI
Connector: Port 1 (P1)
Channels faulted: 0x200
First channel: 10 of 32, counting from 1
Thanks in advance for any pointers!
Try the following (with the convenient adaptations)
#!/usr/bin/perl
use strict;
$/="\nEN "; # register separated by "\nEN "
my $error=2274021; # the error!
while(<>){ # for all registers
next unless /\b$error\b/; # ignore unless error
for my $line ( split(/\n/,$_)){
print "$line\n" if ($line =~ /Coil:|Connector:|Channels faulted:|First channel:/);
}
print "====\n"
}
Is this what you need?

Excel to matlab timestamps

I have data in excel in form of timestamps s it looks like
30/11/12 12:42 AM
30/11/12 12:47 AM
30/11/12 12:56 AM
30/11/12 1:01 AM
I need to get it to matlab to look like this
dateStrings = {...
'30/11/12 12:42 AM' ...
'30/11/12 12:47 AM' ...
'30/11/12 12:56 AM' ...
'30/11/12 1:01 AM' ...
};
I have tried xlsread but it doesn't put in the strings.
The following works for me (in Octave, but should be the same in MATLAB):
>> [num,txt,raw]=xlsread('dates.xls','A1:A4')
num =
4.1243e+004
4.1243e+004
4.1243e+004
4.1243e+004
txt = {}(0x0)
raw =
{
[1,1] = 4.1243e+004
[2,1] = 4.1243e+004
[3,1] = 4.1243e+004
[4,1] = 4.1243e+004
}
>> datestr(num+datenum(1900,1,1,0,0,0)-2)
ans =
30-Nov-2012 00:42:00
30-Nov-2012 00:47:00
30-Nov-2012 00:56:00
30-Nov-2012 01:01:00
>> whos ans
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
ans 4x20 80 char
Total is 80 elements using 80 bytes
Check out the datestr function for the various output format options.
Arnaud
I manage to find a way how to solve it
1. Copy and paste your dates into Excel in dd-mm-yyyy format
2. In Excel, highlight the data and go Right Click, Format Cells/Number
3. In Matlab go a=xlsread(xlsfile);
4. Type datestr(a+693960)

Resources