line = ['R2', 'R5', 'R6', 'R7', 'R10', 'R12', 'R13', 'R15', 'R18', 'R20', 'R21', 'R22', 'R23', 'R24', 'R26', 'R30', 'R31', 'R32', 'R33', 'R34', 'R35', 'R36', 'R37', 'R38', 'R44', 'R45', 'R46', 'R47', 'R48', 'R53', 'R55', 'R56', 'R63', 'R66', 'R68', 'R69', 'R70', 'R74', 'R78', 'R80', 'R81', 'R82', 'R83', 'R84', 'R90', 'R91', 'R92', 'R97', 'R98', 'R99', 'R100', 'R101', 'R102', 'R103', 'R108', 'R109', 'R111', 'R115', 'R116', 'R117', 'R118', 'R120', 'R121', 'R123', 'R124', 'R126', 'R130', 'R131', 'R132', 'R136', 'R138', 'R141', 'R142', 'R151', 'R152', 'R153', 'R154', 'R155', 'R156', 'R158', 'R160', 'R161', 'R163', 'R164', 'R167', 'R169', 'R171', 'R172', 'R177', 'R178', 'R179', 'R180', 'R181', 'R182', 'R183', 'R184', 'R186', 'R190', 'R192', 'R195', 'R196', 'R199', 'R201', 'R203', 'R204', 'R205', 'R206', 'R207', 'R209', 'R210', 'R211', 'R213', 'R214', 'R215', 'R216', 'R217', 'R223', 'R225', 'R227', 'R228', 'R229', 'R231', 'R233', 'R237', 'R238', 'R239', 'R240', 'R245', 'R246', 'R247', 'R248', 'R249', 'R250', 'R252', 'R253', 'R254', 'R255', 'R257', 'R258', 'R265', 'R266', 'R267', 'R268', 'R270', 'R271', 'R273', 'R275', 'R276', 'R277', 'R279', 'R280', 'R281', 'R284', 'R287', 'R290', 'R291', 'R292', 'R293', 'R294', 'R296', 'R299', 'R300', 'R301', 'R303', 'R304', 'R305', 'R306', 'R307', 'R308', 'R309', 'R310', 'R312', 'R313', 'R314', 'R315', 'R317', 'R320', 'R321', 'R323', 'R325', 'R327', 'R329', 'R330', 'R331', 'R334', 'R335', 'R336', 'R339', 'R340', 'R341', 'R343', 'R344', 'R345', 'R347', 'R349', 'R350', 'R352', 'R354', 'R356', 'R358', 'R359', 'R360', 'R361', 'R362', 'R363', 'R365', 'R367', 'R372', 'R373', 'R376', 'R378', 'R379', 'R381', 'R384', 'R386', 'R388', 'R393', 'R397', 'R398', 'R399', 'R401', 'R402', 'R403', 'R405', 'R406', 'R407', 'R410', 'R411', 'R413', 'R414', 'R416', 'R419', 'R420', 'R421', 'R422', 'R427', 'R430', 'R433', 'R434', 'R435', 'R438', 'R439', 'R443', 'R444', 'R445', 'R453', 'R454', 'R455', 'R456', 'R458', 'R459', 'R460', 'R461', 'R462', 'R463', 'R464', 'R466', 'R467', 'R468', 'R469', 'R470', 'R473', 'R475', 'R476', 'R478', 'R481', 'R482', 'R483', 'R485', 'R487', 'R488', 'R489', 'R490', 'R492', 'R494', 'R496', 'R497', 'R499', 'R500', 'R502', 'R503', 'R506', 'R508', 'R509', 'R511', 'R512', 'R516', 'R517', 'R519', 'R520', 'R528', 'R530', 'R532', 'R537', 'R539', 'R540', 'R541', 'R542', 'R544', 'R545', 'R547', 'R549', 'R550', 'R552', 'R555', 'R558', 'R560', 'R561', 'R562', 'R563', 'R564', 'R565', 'R566', 'R568', 'R571', 'R572', 'R573', 'R574', 'R576', 'R579', 'R581', 'R583', 'R584', 'R585', 'R586', 'R588', 'R590', 'R592', 'R593', 'R595', 'R597', 'R598', 'R600', 'R601', 'R606', 'R607', 'R609', 'R610', 'R612', 'R613', 'R614', 'R615', 'R617', 'R618', 'R619', 'R620', 'R622', 'R624', 'R625', 'R626', 'R627', 'R628', 'R629', 'R630', 'R631', 'R632', 'R633', 'R634', 'R635', 'R636', 'R638', 'R642', 'R644', 'R645', 'R646', 'R647', 'R648', 'R649', 'R650', 'R652', 'R654', 'R657', 'R658', 'R660', 'R662', 'R668', 'R669', 'R671', 'R672', 'R673', 'R674', 'R676', 'R677', 'R678', 'R683', 'R684', 'R686', 'R689', 'R691', 'R693', 'R694', 'R696', 'R697', 'R698', 'R700', 'R701', 'R704', 'R706', 'R707', 'R710', 'R712', 'R713', 'R715', 'R716', 'R718', 'R719', 'R721', 'R724', 'R725', 'R728', 'R729', 'R730', 'R731', 'R733', 'R734', 'R737', 'R738', 'R739', 'R740', 'R743', 'R744', 'R745', 'R747', 'R748', 'R751', 'R755', 'R756', 'R757', 'R758', 'R759', 'R761', 'R762', 'R763', 'R767', 'R768', 'R770', 'R773', 'R774', 'R775', 'R776', 'R777', 'R779', 'R783', 'R785', 'R786', 'R789', 'R791', 'R795', 'R796', 'R797', 'R798', 'R800', 'R801', 'R802', 'R804', 'R805', 'R806', 'R808', 'R818', 'R819', 'R820', 'R822', 'R824', 'R826', 'R827', 'R829', 'R830', 'R831', 'R833', 'R835', 'R836', 'R838', 'R840', 'R841', 'R843', 'R844', 'R845', 'R847', 'R848', 'R849', 'R850', 'R851', 'R852', 'R853', 'R855', 'R858', 'R859', 'R860', 'R862', 'R863', 'R865', 'R867', 'R868', 'R869', 'R870', 'R874', 'R875', 'R876', 'R880', 'R881', 'R882', 'R884', 'R885', 'R886', 'R889', 'R891', 'R892', 'R893', 'R894', 'R895', 'R897', 'R900', 'R906', 'R908', 'R909', 'R911', 'R912', 'R914', 'R917', 'R918', 'R921', 'R922', 'R924', 'R925', 'R926', 'R927', 'R928', 'R929', 'R930', 'R932', 'R935', 'R937', 'R943', 'R944', 'R945', 'R946', 'R947', 'R948', 'R949', 'R951', 'R952', 'R953', 'R956', 'R957', 'R958', 'R959', 'R961', 'R964', 'R965', 'R966', 'R967', 'R968', 'R969', 'R971', 'R972', 'R974', 'R975', 'R976', 'R979', 'R981', 'R982', 'R983', 'R986', 'R987', 'R988', 'R990', 'R993', 'R994', 'R997']
ref = str(line).replace("R","")
print(ref)
it prints ('2', '5', '6', '7', '10', '12', '13', '15', '18', '20', '21', '22', '23', '24', '26', '30', '31', '32', '33', ...... and so on
Would like to turn to integers(2,5,6,7,10,12,13,15,18... and so on. I have tried but no good results.
Use list comprehension
line = [int(l.replace("R", "")) for l in line]
If "R" is always the first character, you can also do:
line = [int(l[1:]) for l in line]
Per the data provided, both solution above will work solve the original question, if you may have invalid data, for example R32R the above will fail and raise a ValueError exception.
To prevent this you can discard any value that are not an integer by using:
item.isnumeric()
line = [int(l[1:]) for l in line if l[1:].isnumeric()]
You can also achieve the same with a regex expression
re.search('\d+', line[0]).group(0)
line = [int(re.search('\d+', l).group(0)) for l in line if re.search('\d+', l) is not None]
You will need the following import:
import re
I need to parse this raw data in order to process it:
Port Align-Err FCS-Err Xmit-Err Rcv-Err UnderSize OutDiscards
Gi0/1 1 2 3 4 5 6
Gi0/2 11 12 13 14 15 16
Port Single-Col Multi-Col Late-Col Excess-Col Carri-Sen Runts Giants
Gi0/1 1 2 3 4 5 6 7
Gi0/2 111 122 133 144 155 166 177
To do this, I'm using TextFSM.
I would want this output:
['Gi0/1', '1', '2', '3', '4', '5', '6', '1', '2', '3', '4', '5', '6', '7']
['Gi0/2', '11', '12', '13', '14', '15', '16', '111', '112', '113', '114', '115', '116', '117']
The first template I wrote is this one:
Value PORT (\S+(/\d+)?)
Value ALIGNERR (\d+)
Value FCSERR (\d+)
Value XMITERR (\d+)
Value RCVERR (\d+)
Value UNDERSIZE (\d+)
Value OUTDISCARDS (\d+)
Value SINGLECOL (\d+)
Value MULTICOL (\d+)
Value LATECOL (\d+)
Value EXCESSCOL (\d+)
Value CARRISEN (\d+)
Value RUNTS (\d+)
Value GIANTS (\d+)
Start
^Port\s+Align-Err.* -> FIRST
^Port\s+Single-Col.* -> SECOND
FIRST
^${PORT}\s+${ALIGNERR}\s+${FCSERR}\s+${XMITERR}\s+${RCVERR}\s+${UNDERSIZE}\s+${OUTDISCARDS} -> Continue.Record
SECOND
^${PORT}\s+${SINGLECOL}\s+${MULTICOL}\s+${LATECOL}\s+${EXCESSCOL}\s+${CARRISEN}\s+${RUNTS}\s+${GIANTS} -> Record
However, the output is not right:
['Gi0/1', '1', '2', '3', '4', '5', '6', '', '', '', '', '', '', '']
['Gi0/2', '11', '12', '13', '14', '15', '16', '', '', '', '', '', '', '']
['Gi0/1', '1', '2', '3', '4', '5', '6', '', '', '', '', '', '', '']
['Gi0/2', '111', '122', '133', '144', '155', '166', '', '', '', '', '', '', '']
I found a post on the forum giving a solution in pure Regex: TextFSM logic - Avoid capturing same data twice
When I adapt it to my needs, I have a match for what I need: https://regex101.com/r/DY0Meb/6
However, I'm unable to translate it in a TextFSM template, it fails.
Here is my template:
Value PORT (\S+(/\d+)?)
Value ALIGNERR (\d+)
Value FCSERR (\d+)
Value XMITERR (\d+)
Value RCVERR (\d+)
Value UNDERSIZE (\d+)
Value OUTDISCARDS (\d+)
Value SINGLECOL (\d+)
Value MULTICOL (\d+)
Value LATECOL (\d+)
Value EXCESSCOL (\d+)
Value CARRISEN (\d+)
Value RUNTS (\d+)
Value GIANTS (\d+)
Start
^${PORT}\s+${ALIGNERR}\s+${FCSERR}\s+${XMITERR}\s+${RCVERR}\s+${UNDERSIZE}\s+${OUTDISCARDS}(?=.*\1\s+${SINGLECOL}\s+${MULTICOL}\s+${LATECOL}\s+${EXCESSCOL}\s+${CARRISEN}\s+${RUNTS}\s+${GIANTS}) -> Record
Any clue about why I don't have any matches?
I'm a beginner in Regex, and I can't seem to find the solution...
Any help would be very welcome :).
Thanks in advance!
There is a bit simpler way exists:
from ttp import ttp
import pprint
data = """
Port Align-Err FCS-Err Xmit-Err Rcv-Err UnderSize OutDiscards
Gi0/1 1 2 3 4 5 6
Gi0/2 11 12 13 14 15 16
Port Single-Col Multi-Col Late-Col Excess-Col Carri-Sen Runts Giants
Gi0/1 1 2 3 4 5 6 7
Gi0/2 111 122 133 144 155 166 177
"""
template = """
<group name="{{ interface }}*">
{{ interface | exclude("Port") }} {{ counters | ROW | resub("\s+", ",", 200) | split(",") }}
</group>
"""
parser = ttp(data, template)
parser.parse()
res = parser.result()[0][0]
# res is this now:
# {'Gi0/1': [{'counters': ['1', '2', '3', '4', '5', '6']},
# {'counters': ['1', '2', '3', '4', '5', '6', '7']}],
# 'Gi0/2': [{'counters': ['11', '12', '13', '14', '15', '16']},
# {'counters': ['111', '122', '133', '144', '155', '166', '177']}]}
ret = []
for interface, counters in res.items():
ret.append([interface])
for i in counters:
ret[-1] += list(i.values())[0]
pprint.pprint(ret, width=100)
# will print:
# [['Gi0/1', '1', '2', '3', '4', '5', '6', '1', '2', '3', '4', '5', '6', '7'],
# ['Gi0/2', '11', '12', '13', '14', '15', '16', '111', '122', '133', '144', '155', '166', '177']]
TTP - is a library I created to make solutions for above tasks to look simpler.
I finally managed to do what I want.
My team wished to use Ansible for the formatting, so I had to improvise a bit.
I used ntc-ansible for that.
With the help of members of the NTC Slack, I finally got it working. Here's what I came up with:
A functionality that is very poorly documented on the TextFSM repo is that you can, in an index file, combine two templates that share a common "Key" attribute.
So I created two templates:
Value Key PORT (\S+(/\d+)+)
Value ALIGNERR (\d+)
Value FCSERR (\d+)
Value XMITERR (\d+)
Value RCVERR (\d+)
Value UNDERSIZE (\d+)
Value OUTDISCARDS (\d+)
Start
^Port\s+Align-Err.* -> Begin
Begin
^${PORT}\s+${ALIGNERR}\s+${FCSERR}\s+${XMITERR}\s+${RCVERR}\s+${UNDERSIZE}\s+${OUTDISCARDS} -> Record
^Port\s+Single-Col.* -> End
And:
Value Key PORT (\S+(/\d+)+)
Value SINGLECOL (\d+)
Value MULTICOL (\d+)
Value LATECOL (\d+)
Value EXCESSCOL (\d+)
Value CARRISEN (\d+)
Value RUNTS (\d+)
Value GIANTS (\d+)
Start
^Port\s+Single-Col.* -> Begin
Begin
^${PORT}\s+${SINGLECOL}\s+${MULTICOL}\s+${LATECOL}\s+${EXCESSCOL}\s+${CARRISEN}\s+${RUNTS}\s+${GIANTS} -> Record
Then, I created an index file containing this:
Template, Hostname, Vendor, Command
show_int_counter_errors1.template:show_int_counter_errors2.template, .*, cisco_ios, sh[[ow]] int[[erfaces]] cou[[nter]] er[[rors]]
You can test it in Python with this little script:
import textfsm
import sys
from textfsm import clitable
# Define Input Data
input_data = sys.stdin.read()
# Initialise CliTable with the content of the 'index' file.
cli_table = clitable.CliTable('index', '.')
# Firstly we will use attributes to match a 'show version' command on a Cisco device.
attributes = {'Command': sys.argv[1], 'Vendor': 'cisco_ios'}
# Parse Data
cli_table.ParseCmd(input_data, attributes)
print(cli_table)
To launch it, just use this command:
python3 test_table.py 'show interface counter errors' < show_int_counter_errors.txt
To use it in Ansible, after installing ntc-ansible, create a 'templates' directory, put the index file and the template files in it, and specify the directory path in the playbook:
- hosts: all
connection: local
gather_facts: no
tasks:
- name: Formatting the errors
ntc_show_command:
connection: ssh
platform: cisco_ios
command: 'show interfaces counter errors'
use_templates: True
template_dir: 'PATH/TO/TEMPLATES/DIRECTORY'
host: "{{ ansible_host }}"
username: "{{ ansible_user }}"
password: "{{ ansible_password }}"
register: interface_errors
- name: Display the registered variable
debug:
var: interface_errors
Hope this can help anybody :).
I'm wondering how I can encode Dicts in Python 2.7. When I do the following:
# coding=ISO-8859-1
Programmer = {'v': ["1","2x","3","4","5"]}
Programmer.update({'x': ["1","2x","3","4","5"]})
Programmer.update({'y': ["1","2x","3","4","5"]})
Programmer.update({'z': ["1","2x","3","4","5"]})
Programmer.update({'æ': ["1","2x","3","4","5"]})
Programmer.update({'ø': ["1","2x","3","4","5"]})
Programmer.update({'å': ["1","2x","3","4","5"]})
print Programmer
I get:
{'v': ['1', '2x', '3', '4', '5'], '\xe5': ['1', '2x', '3', '4', '5'], '\xf8': ['1', '2x', '3', '4', '5'], '\xe6': ['1', '2x', '3', '4', '5'], 'y': ['1', '2x', '3', '4', '5'], 'x': ['1', '2x', '3', '4', '5'], 'z': ['1', '2x', '3', '4', '5']}
And what I want is:
{'v': ['1', '2x', '3', '4', '5'], 'å': ['1', '2x', '3', '4', '5'], 'ø': ['1', '2x', '3', '4', '5'], 'æ': ['1', '2x', '3', '4', '5'], 'y': ['1', '2x', '3', '4', '5'], 'x': ['1', '2x', '3', '4', '5'], 'z': ['1', '2x', '3', '4', '5']}
EDIT:
This is my working code:
# -*- coding: cp1252 -*-
from PyQt4 import QtCore, QtGui
import locale
global Programmer
Programmer = {u'v': ["1","2x","3","4","5"]}
Programmer.update({u'x': ["1","2x","3","4","5"]})
Programmer.update({u'y': ["1","2x","3","4","5"]})
Programmer.update({u'z': ["1","2x","3","4","5"]})
Programmer.update({u'æ': ["1","2x","3","4","5"]})
Programmer.update({u'ø': ["1","2x","3","4","5"]})
Programmer.update({u'å': ["1","2x","3","4","5"]})
class Dialog(QtGui.QDialog):
NumGridRows = 3
NumButtons = 3
def __init__(self):
super(Dialog, self).__init__()
styleComboBox = QtGui.QComboBox()
styleComboBox.addItem("UI Item01")
styleLabel = QtGui.QLabel("&UI Text:")
styleLabel.setBuddy(styleComboBox)
opgaveComboBox = QtGui.QComboBox()
opgaveComboBox.addItem(u"v")
opgaveComboBox.addItem(u"x")
opgaveComboBox.addItem(u"å")
opgaveLabel = QtGui.QLabel("&Opgave:")
opgaveLabel.setBuddy(opgaveComboBox)
self.programComboBox = QtGui.QComboBox()
self.programComboBox.addItem("UI item02")
programLabel = QtGui.QLabel("&Program:")
programLabel.setBuddy(self.programComboBox)
opgaveComboBox.activated[str].connect(self.changeOpgave)
topLayout = QtGui.QHBoxLayout()
topLayout.addWidget(styleLabel)
topLayout.addWidget(styleComboBox)
#topLayout.addStretch(1)
topLayoutB = QtGui.QHBoxLayout()
topLayoutB.addWidget(opgaveLabel)
topLayoutB.addWidget(opgaveComboBox)
#topLayoutB.addStretch(1)
topLayoutC = QtGui.QHBoxLayout()
topLayoutC.addWidget(programLabel)
topLayoutC.addWidget(self.programComboBox)
mainLayout = QtGui.QGridLayout()
mainLayout.addLayout(topLayout, 0, 0, 1, 2)
mainLayout.addLayout(topLayoutB, 1, 0, 1, 2)
mainLayout.addLayout(topLayoutC, 2, 0, 1, 2)
mainLayout.setRowStretch(1, 1)
mainLayout.setRowStretch(2, 1)
mainLayout.setColumnStretch(0, 1)
mainLayout.setColumnStretch(1, 1)
self.setLayout(mainLayout)
self.changeStyle('Cleanlooks')
self.setWindowTitle("temp test")
def changeOpgave(self, opgaveName):
self.programComboBox.clear()
print "begin 01"
print Programmer[opgaveName]
if not opgaveName in Programmer:
print "begin 02"
self.programComboBox.addItem("Andet")
else:
print "go"
for item in Programmer[opgaveName]:
self.programComboBox.addItem(item)
def changeStyle(self, styleName):
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create(styleName))
self.changePalette()
def changePalette(self):
QtGui.QApplication.setPalette(QtGui.QApplication.style().standardPalette())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
dialog = Dialog()
dialog.show()
sys.exit(dialog.exec_())
I get
KeyError: PyQt4.QtCore.QString(u'v')
KeyError: PyQt4.QtCore.QString(u'x')
KeyError: "PyQt4.QtCore.QString(u'\xe5')
When trying to select from the "opgave" list
(error thrown at line 79)
The combobox callback is handed a PyQt4.QtCore.QString() object, not a unicode string. Your dictionary on the other hand should use unicode() keys (which is the right thing to do).
Convert the QString() object to a unicode value first:
def changeOpgave(self, opgaveName):
opgaveName = unicode(opgaveName)
This works because the QString() object already holds unicode data and provides Python with the right hooks to make this conversion without needing to re-encode the data with an explicit codec.
Once the opgaveName value is a unicode key, you can use it to compare it against your dictionary.
Note that you can just define the dictionary without .update() calls:
Programmer = {
u'v': ["1","2x","3","4","5"],
u'x': ["1","2x","3","4","5"],
u'y': ["1","2x","3","4","5"],
u'z': ["1","2x","3","4","5"],
u'æ': ["1","2x","3","4","5"],
u'ø': ["1","2x","3","4","5"],
u'å': ["1","2x","3","4","5"],
}
The global keyword has no meaning where you are using it; that keyword only makes sense inside functions, and is redundant here.
may be like this:
print "{",
for key, value in Programmer.items():
print "'%s'" % key, ':', value, ',',
print "}"
when you do print then this will print str(Programmers) but inside dict uses repr(key) and repr(value). The content does not change, it will just be printed differently.