Ich habe eine Eingabedatei, dessen Kopf wie folgt aussieht:Split-Zeile auf Komma, aber nicht Komma in Anführungszeichen?
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},
ich Parse versuche jede Zeile in seinem eigenen Objekt. Allerdings kann ich aufgeteilt auf Kommas nicht, weil einige der Beschreibungen Kommas in ihnen haben ..
versucht, diese zwei regex Linien von den Stackoverflow Beiträge die ich finden konnte:
re.split(r', (?=(?:"[^"]*?(?: [^"]*)*))|, (?=[^",]+(?:,|$))', content[x])
Und:
[y.strip() for y in content[x].split(''',(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''')]
jedoch .. sie beide Ausgangs
['{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.",
Irgendwelche Ideen, wie kann ich dieses Problem beheben Es teilt also jede Zeile korrekt in drei separate Abschnitte statt nur zwei? Danke
Es ist mir unklar, was Sie mit "Objekt" hier meinen. Versuchen Sie, eine Liste von Python-Wörterbüchern zu erstellen? –
@DavidC Yup! Es sieht so aus, als ob Brads Lösung mich mit ein bisschen Herumspielen erreichen würde, obwohl – knave