【发布时间】: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