我有它的頭看起來像這樣的輸入文件:用逗號分割線,但在引號內不用逗號?
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 Yup!它看起來像布拉德的解決方案會讓我有一點點弄亂,雖然 – knave