【问题标题】:Split line on comma but not comma within quotes?逗号分割线但引号内没有逗号?
【发布时间】:2017-05-11 20:52:43
【问题描述】:

我有一个头部如下所示的输入文件:

AdditionalCookout.create!([
  {day_id: 275, cookout_id: 71, description: "Sample text, that, is ,driving , me, crazy"},
  {day_id: 275, cookout_id: 87, description: nil},
  {day_id: 276, cookout_id: 71, description: nil},
  {day_id: 276, cookout_id: 87, description: nil},
  {day_id: 277, cookout_id: 92, description: nil},
  {day_id: 277, cookout_id: 71, description: nil},

我正在尝试将每一行解析为它自己的对象。但是,我不能用逗号分开,因为有些描述里面有逗号..

从我可以找到的 StackOverflow 帖子中尝试了这两条正则表达式:

re.split(r', (?=(?:"[^"]*?(?: [^"]*)*))|, (?=[^",]+(?:,|$))', content[x])

还有:

[y.strip() for y in content[x].split(''',(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''')]

但是..他们都输出

['{day_id: 275', 'cookout_id: 71, description: "Feeling ambitious? If you really want to exhaust yourself today, consider adding some additional stationary cardio."},']

Turns into:
day_id: 275
cookout_id: 71, description: "Feeling ambitious? If you really want to exhaust yourself today, consider adding some additional stationary cardio.",

有什么想法可以解决这个问题,以便正确地将每一行分成三个单独的部分,而不是两个?谢谢

【问题讨论】:

  • 我不清楚这里的“对象”是什么意思。您是否正在尝试创建 python 字典列表?
  • @DavidC 是的!看起来布拉德的解决方案会让我有点混乱

标签: python regex python-2.7 parsing split


【解决方案1】:

尝试使用 PyYAML 来解析它。在你的例子上从我那里工作。 https://pypi.python.org/pypi/PyYAML。那么你就可以避免正则表达式的头痛了。

import yaml
yaml.load('{day_id: 275, cookout_id: 71, description: "Sample text, that, is,driving , me, crazy"}')
{'cookout_id': 71,
 'day_id': 275,
 'description': 'Sample text, that, is,driving , me, crazy'}

【讨论】:

  • 优秀的答案!您也可以将其应用于词典列表,它可以工作。好吧,也许是因为输入文件 yaml。但无论如何。
  • 太棒了!我正在逐行阅读文件,因为其中嵌入了许多诸如“AdditionalCookout”之类的类,它们都有自己的字典列表..但这将完成工作,但会有些混乱!谢谢=D
  • 点@Jean-FrançoisFabre。
猜你喜欢
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
  • 2012-07-12
  • 1970-01-01
  • 2012-05-23
  • 2017-04-07
  • 2022-03-18
  • 1970-01-01
相关资源
最近更新 更多