Python、字符串、unicode 字符

发布于 2024-12-03 03:50:04 字数 166 浏览 2 评论 0原文

comp/INFO_MAP_ECE/101102.1.119

该字符串是CPU的输出,但数字后面总是有特殊/不可打印的字符,我的目标是获取不包括前面的文本和特殊字符的数字/ 之后不可打印。我正在尝试 split 方法,但不确定如何用于特殊/不可打印字符。有人可以建议一下吗?这将是一个很大的帮助。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2024-12-10 03:50:04

假设您的输出总是看起来像您所显示的那样,您可以使用 正则表达式

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

:这里的模式查找 /,然后是一些数字和句点,然后是任何内容,然后仅提取数字和句点。只要您始终获得与该描述匹配的字符串,这就应该有效。

哈!

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!

孤蝉 2024-12-10 03:50:04

数字的长度总是相同吗?如果是这样,你可以直接切片字符串。

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

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

'comp/INFO_MAP_ECE/101102.1.119'[18:30]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文