python中的TCL多字符分开?

发布于 2025-01-19 17:38:47 字数 633 浏览 1 评论 0原文

我正在使用此 tcl 过程拆分文本文件:

proc mcsplit "str splitStr {mc {\x00}}" {
return [split [string map [list $splitStr $mc] $str] $mc]  }

# mcsplit --
#   Splits a string based using another string
# Arguments:
#   str       string to split into pieces
#   splitStr  substring
#   mc        magic character that must not exist in the orignal string.
#             Defaults to the NULL character.  Must be a single character.
# Results:
#   Returns a list of strings

split 命令根据 splitString 中的每个字符拆分字符串。此版本将 splitString 作为组合字符串处理,将字符串拆分为组成部分, 但我的目标是使用 python 做同样的事情,这里有人以前做过同样的事情吗?

I am splitting a text file using this tcl proc:

proc mcsplit "str splitStr {mc {\x00}}" {
return [split [string map [list $splitStr $mc] $str] $mc]  }

# mcsplit --
#   Splits a string based using another string
# Arguments:
#   str       string to split into pieces
#   splitStr  substring
#   mc        magic character that must not exist in the orignal string.
#             Defaults to the NULL character.  Must be a single character.
# Results:
#   Returns a list of strings

The split command splits a string based on each character that is in the splitString. This version handles the splitString as a combined string, splitting the string into constituent parts,
but my objective is to do the same using python does anyone here did the same before?

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

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

发布评论

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

评论(1

不回头走下去 2025-01-26 17:38:47

从您的问题中还不清楚 python split 行为是否是您所需要的。如果您需要在每次出现多字符字符串时进行拆分,Python 的常规 split 就可以完成这项工作:

>>> 'this is a test'.split('es')
['this is a t', 't']

但是,如果您希望在出现多个单个时进行拆分> 字符,您需要使用 re.split

>>> import re
>>> re.split(r'[es]', 'this is a test')
['thi', ' i', ' a t', '', 't']
>>>

It's not very clear from your question whether the python split behavior is what you need. If you need to split at each occurrence of a multiple-character string, Python's regular split will do the job:

>>> 'this is a test'.split('es')
['this is a t', 't']

If, however, you want to split at any occurrence of multiple individual characters, you'll need to use re.split:

>>> import re
>>> re.split(r'[es]', 'this is a test')
['thi', ' i', ' a t', '', 't']
>>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文