Reply

Trouble with substring in python

Techie
Posts: 8
2638     0

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
' ***

Re: Trouble with substring in python

Superuser
Posts: 115
2638     0

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

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

https://sifbaksh.com

Re: Trouble with substring in python

New Member
Posts: 1
2638     0

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 (Smiley Happy 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.

 

Showing results for 
Search instead for 
Did you mean: 

Recommended for You