gpt4 book ai didi

python-3.x - 仅当引号包含 3 个或更多字符时才拆分

转载 作者:行者123 更新时间:2023-12-05 05:40:26 24 4
gpt4 key购买 nike

我试图在出现两个引号并且它们包含至少 3 个字符时拆分给定的字符串(我也必须在 . 或 , 出现时拆分)。所以,像 hello"example"hello,cat 这样的东西应该返回 [hello;example;hello;cat]。我想到了:

re.split("\'(...+)\'|\.|,","hello'example'hello,cat")

这适用于引号,但每当它拆分为 .或者,这会发生:

['hello', 'example', 'hello', None, 'cat']

我发现捕获组是导致它的原因(列表中间的 None),但这是我知道的保留内容的唯一方法。请记住,我必须做尽可能少的计算,因为该程序将处理大文件,而且我对 Python 不是很有经验,如果我做错了一些明显的事情,我深表歉意。

最佳答案

尝试一下:

re.split("\'|\.|,", "hello'example'hello,cat")

关于python-3.x - 仅当引号包含 3 个或更多字符时才拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72431605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com