- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
Trouble with substring in python
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2020 04:56 PM
Hello,
Has anyone tried to manipulate strings in python with NetMRI?
Basically, I'm trying to pull the first 4 characters of the device's name and stored it in a new variable. NetMRI gives me an error but the same python code works in a another Linux environment. Any workaround for this?
This code works fine in another Linux platform:
>>> device = 'baltrouter1'
>>> print(device[:4].upper())
BALT
>>>
This python code (2nd print command) doesn't run in NetMRI:
mydevice = 'baltrouter1'
print(mydevice)
print(mydevice[:4].upper())
NetMRI Status Logs:
+++ Looking up device information ...................................... OK
+++ Looking up device information ...................................... OK
+++ Looking up job specification information ........................... OK
+++ Loading ccs file ................................................... OK
+++ Script: script_xyz
+++ Script-Filter ...................................................... MATCH
baltrouter1
*** ERROR: Error 'Traceback (most recent call last):
File "<stdin>", line 170, in <module>
TypeError: 'str' object is not callable
Python script has returned 1 value
' ***
Solved! Go to Solution.
Re: Trouble with substring in python
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2020 06:57 AM
I just ran the following and got these results:
Code:
# Create NetMRI context manager. It will close session after execution with NetMRIEasy(**defaults) as easy: mydevice = 'baltrouter1' print(mydevice) print(mydevice[:4].upper())
Results:
+++ Script: f +++ Script-Filter ...................................................... MATCH baltrouter1 BALT +++ Looking up device information ...................................... OK
I'm running 7.4.2
Twitter: https://twitter.com/sifbaksh
https://sifbaksh.com
Re: Trouble with substring in python
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2021 12:42 AM
Python has no substring methods like python substring() or substr(). Instead, you can use slice syntax to get parts of existing strings. Python slicing is a computationally fast way to methodically access parts of your data. The colons ( in subscript notation make slice notation - which has the arguments, start, stop and step . It follows this template:
Parameters are enclosed in the square brackets.
Parameters are separated by colon.
string[start: end: step]
- start - Starting index of string, Default is 0.
- end - End index of string which is not inclusive .
- step - An integer number specifying the step of the slicing. Default is 1.