简体   繁体   中英

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.

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM