开发者

Python, strings, unicode characters

开发者 https://www.devze.com 2023-04-03 11:12 出处:网络
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 i

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 metho开发者_如何学Pythond 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]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号