使用 Python 对字符串中的文本进行解析和分组
我需要解析一系列短字符串,这些字符串由 3 个部分组成:一个问题和 2 个可能的答案。该字符串将遵循一致的格式:
这是问题 "answer_option_1 is inquotes" "answer_option_2 is inquotes"
我需要识别问题部分以及单引号或双引号中的两个可能的答案选项。
前任。: 今天的天空是什么颜色? “蓝色”或“灰色”
谁将赢得“密歇根州”“俄亥俄州”比赛
我如何在 python 中做到这一点?
I need to parse a series of short strings that are comprised of 3 parts: a question and 2 possible answers. The string will follow a consistent format:
This is the question "answer_option_1 is in quotes" "answer_option_2 is in quotes"
I need to identify the question part and the two possible answer choices that are in single or double quotes.
Ex.:
What color is the sky today? "blue" or "grey"
Who will win the game 'Michigan' 'Ohio State'
How do I do this in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的格式如您所说的简单(即不是,如您的示例中所示),则不需要正则表达式。只需
分割
该行:If your format is a simple as you say (i.e. not as in your examples), you don't need regex. Just
split
the line:一种可能性是您可以使用正则表达式。
输出:
One possibility is that you can use regex.
Output:
Pyparsing 将为您提供一个适应输入文本中的某些变化的解决方案:
将打印:
Pyparsing will give you a solution that will adapt to some variability in the input text:
Will print: