Python 操作字符串
我有一个“原始”变量,用于存储:
*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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 100% 确定我是否理解你的问题,但我猜下面的代码可能会帮助你:
给出以下输出:
这是你需要的吗?
I'm not 100% sure if I understand your question, but I'm guessing the following code might help you:
gives the following output:
Is that what you need?
在我看来,您只需要通过
"
分隔符分割字符串并获取所有其他子字符串(因为有趣的子字符串将与空格交织在一起):测试似乎会产生正确的结果:
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):Testing appears to yield correct results: