我有一个输入文件,其头部如下所示:
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/