kishore
kishore

Reputation: 11

Python, strings, unicode characters

comp/INFO_MAP_ECE/101102.1.119

This string is the output of a CPU but there are always special/non-printable characters after the number and my aim is to obtain the number excluding the text before it and special/non-printable after it. I am trying the split method but am not sure what to use for special/non-printable characters. Can anyone please suggest something? It would be a great help. thanks.

Upvotes: 1

Views: 211

Answers (2)

Jordan Bouvier
Jordan Bouvier

Reputation: 308

Is the number always the same length? If so you could just slice the string.

'comp/INFO_MAP_ECE/101102.1.119'[18:30]

Upvotes: 1

andronikus
andronikus

Reputation: 4220

Assuming your output always looks something like what you showed, you can use a regular expression:

numPattern = r'/([\d.]+)'
output = 'comp/INFO_MAP_ECE/101102.1.119'

m = re.search(numPattern, output)

if m: #If a match was found
  numString = m.group(1)  #Extracts the first group surrounded by ()
  #etc

The pattern here looks for a /, then some numbers and periods, then anything, and extracts just the numbers and periods. This should work as long as you're always getting a string that matches that description.

HTH!

Upvotes: 3

Related Questions