Python 操作字符串

发布于 2024-12-12 12:58:52 字数 709 浏览 0 评论 0原文

我有一个“原始”变量,用于存储:

*rawVariable =* "Hello" "World"  "String 1"  "String 2"  "String 3"  "Sting 4"   
"Hello" "World"  "String 5"  "String 6"  "String 7"  "String 8"   
"Hello" "World" "String 9"  "String 10"  "String 11"  "String 12"

是否可以将单词存储在数组中,并以前后双引号作为分隔符?

我无法使用 " 作为分隔符。

示例:

formattedArray = ["Hello","World","String 1","String 2","String
3","Sting 4","Hello","World"  "String 5","String 6","String 7","String
8","Hello","World","String 9","String 10","String 11","String 12"]

注意:

  • 之间有不同的间距(包括 \n) 。
  • 我将在 formattedArray 之后存储到 2D 数组中

I have a "raw" variable that store:

*rawVariable =* "Hello" "World"  "String 1"  "String 2"  "String 3"  "Sting 4"   
"Hello" "World"  "String 5"  "String 6"  "String 7"  "String 8"   
"Hello" "World" "String 9"  "String 10"  "String 11"  "String 12"

Is it possible that I store the word in a array with front and back double quotes as a delimiter?

I unable to use " as delimiter.

Example:

formattedArray = ["Hello","World","String 1","String 2","String
3","Sting 4","Hello","World"  "String 5","String 6","String 7","String
8","Hello","World","String 9","String 10","String 11","String 12"]

Note:

  • There are different spacing (including \n) between the word.
  • I will be storing into 2D array after the formattedArray.

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

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

发布评论

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

评论(2

魔法唧唧 2024-12-19 12:58:52

我不是 100% 确定我是否理解你的问题,但我猜下面的代码可能会帮助你:

import re

def splitRawString(s):
    return map(lambda x: re.sub('^"?([^"]*)"?

给出以下输出:

['Hello', 'World', 'String 1', 'String 2', 'String 3', 'Sting 4', 'Hello', 'World', 'String 5', 'String 6', 'String 7', 'String 8', 'Hello', 'World', 'String 9', 'String 10', 'String 11', 'String 12']

这是你需要的吗?

, r'\1', x), re.split('"\s*"', s)) a='"Hello" "World" "String 1" "String 2" "String 3" "Sting 4" "Hello" "World" "String 5" "String 6" "String 7" "String 8" "Hello" "World" "String 9" "String 10" "String 11" "String 12"' print splitRawString(a)

给出以下输出:

这是你需要的吗?

I'm not 100% sure if I understand your question, but I'm guessing the following code might help you:

import re

def splitRawString(s):
    return map(lambda x: re.sub('^"?([^"]*)"?

gives the following output:

['Hello', 'World', 'String 1', 'String 2', 'String 3', 'Sting 4', 'Hello', 'World', 'String 5', 'String 6', 'String 7', 'String 8', 'Hello', 'World', 'String 9', 'String 10', 'String 11', 'String 12']

Is that what you need?

, r'\1', x), re.split('"\s*"', s)) a='"Hello" "World" "String 1" "String 2" "String 3" "Sting 4" "Hello" "World" "String 5" "String 6" "String 7" "String 8" "Hello" "World" "String 9" "String 10" "String 11" "String 12"' print splitRawString(a)

gives the following output:

Is that what you need?

趁微风不噪 2024-12-19 12:58:52

在我看来,您只需要通过 " 分隔符分割字符串并获取所有其他子字符串(因为有趣的子字符串将与空格交织在一起):

def split_quoted_strings(s):
    split_via_quote = s.split('"')
    return split_via_quote[1::2]

测试似乎会产生正确的结果:

>>> a='"Hello" "World" "String 1" "String 2" "String 3" "Sting 4" "Hello" "World" "String 5" "String 6" "String 7" "String 8" "Hello" "World" "String 9" "String 10" "String 11" "String 12"'
>>> split_quoted_string(a)
['Hello',
'World',
# omitted
'String 12']

It seems to me that you just need to split the string via " delimeter and get every other substring (since the interesting ones will be interwined with whitespace):

def split_quoted_strings(s):
    split_via_quote = s.split('"')
    return split_via_quote[1::2]

Testing appears to yield correct results:

>>> a='"Hello" "World" "String 1" "String 2" "String 3" "Sting 4" "Hello" "World" "String 5" "String 6" "String 7" "String 8" "Hello" "World" "String 9" "String 10" "String 11" "String 12"'
>>> split_quoted_string(a)
['Hello',
'World',
# omitted
'String 12']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文