python - 用逗号分割线但不在引号内用逗号?

标签 python regex python-2.7 parsing split

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

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.",

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

最佳答案

尝试使用 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'}

关于python - 用逗号分割线但不在引号内用逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43925328/

相关文章:

python - 衍生进程中的操作系统环境变量读取

python - 凯撒密码的实现方法

python - 通过 Homebrew 并行安装 Python 2.7 和 3.3 - pip3 失败

javascript - RegExp "/g"不适用于 string.search

regex - Linux grep 表达式停止匹配,直到找到一个 windows 回车符

python - 在 Python 2.7 中合并具有字典列表的字典

python - 使用 protobufs 与 python ImportError : cannot import name descriptor_pb2 的问题

python - 需要下划线的否定环视或否定断言的正则表达式

python-2.7 - Python 中的 Tarantool - 如何调用 auto_increment 函数

python-2.7 - 如何从数据库中获取数据并在 kivy+python 中显示在表中