Reputation: 559
I have seen this: Python parse CSV ignoring comma with double-quotes
And many like it. But it doesn't do anything to my text. I already have it in a list of one, I need it to be broken based on those commas outside the double quotes.
Here is some sample text and my code:
lines = '''"This is not your grave... but you are welcome in it.","I am a monument to all your sins.","There is much talk, and I have listened, through rock and metal and time. Now I shall talk, and you shall listen.","If you will not hear the truth, then I will show it to you.","Arrogant creatures! Your deaths will be instantaneous, while we shall suffer the progress of infinitude!","We exist together now, many corpses in one grave...","Silence fills the empty grave, now that I have gone. But my mind is not at rest, for questions linger on. I will ask, and you will answer.","Do not be afraid. I am peace; I am salvation.","I am a timeless chorus. Join your voice with mine, and sing victory everlasting.","Do not shoot, but listen! Let me lead you safely... to our foe. Only you can halt... what he has set in motion.","Lies for the weak! Beacons for the deluded!","You will be food; nothing more...","Now the gate has been unlatched, headstones pushed aside; corpses shift and offer room, a fate you must abide!","Child of my enemy, why have you come? I offer no forgiveness, a father's sins, passed to his son.","Of course, you came for her. We exist together now, two corpses in one grave.","And yet, perhaps a part of her remains...","Time... has taught me patience! But basking in new freedom, I will know all that I possess!","Submit! End her torment and my own!","You will show me what she hides! Or I shall feast upon your BONES!","I have consumed a galaxy of flesh and mind and bone!","Do I take life or give it? Who is victim, and who is foe?","Resignation is my virtue; like water I ebb, and flow. Defeat is simply the addition of time to a sentence I never deserved... but you imposed.","From end to end of this galaxy, all are blind!","Side by side we march as one. Humans and Elites will die. The Earth will fall if we strike together. And so forth shall all of life.","All life dies, all worlds too, and if there is guaranteed perpetual existence, after that - what does it matter how the end comes?","All consumption is death for the consumed. Yet all must eat, so we all bring damnation to one creature or another."'''.splitlines()
from csv import reader
from csv import QUOTE_ALL
phrases = []
for l in reader(lines, quotechar='"', delimiter=',',quoting=QUOTE_ALL, skipinitialspace=True):
phrases.append(l)
However, the end result of this is that I have a list of length 1, which doesn't do me any good.
I really don't know how to get it so every sentence becomes it's own entry in a list. I'm sure this is a duplicate, but I've been looking for a while and can't find anyone with this problem of it turning into a list of length 1.
Upvotes: 0
Views: 92
Reputation: 51683
Strip starting and ending "
, split at '","'
:
text = '''"This is not your grave... but you are welcome in it.","I am a monument to all your sins.","There is much talk, and I have listened, through rock and metal and time. Now I shall talk, and you shall listen.","If you will not hear the truth, then I will show it to you.","Arrogant creatures! Your deaths will be instantaneous, while we shall suffer the progress of infinitude!","We exist together now, many corpses in one grave...","Silence fills the empty grave, now that I have gone. But my mind is not at rest, for questions linger on. I will ask, and you will answer.","Do not be afraid. I am peace; I am salvation.","I am a timeless chorus. Join your voice with mine, and sing victory everlasting.","Do not shoot, but listen! Let me lead you safely... to our foe. Only you can halt... what he has set in motion.","Lies for the weak! Beacons for the deluded!","You will be food; nothing more...","Now the gate has been unlatched, headstones pushed aside; corpses shift and offer room, a fate you must abide!","Child of my enemy, why have you come? I offer no forgiveness, a father's sins, passed to his son.","Of course, you came for her. We exist together now, two corpses in one grave.","And yet, perhaps a part of her remains...","Time... has taught me patience! But basking in new freedom, I will know all that I possess!","Submit! End her torment and my own!","You will show me what she hides! Or I shall feast upon your BONES!","I have consumed a galaxy of flesh and mind and bone!","Do I take life or give it? Who is victim, and who is foe?","Resignation is my virtue; like water I ebb, and flow. Defeat is simply the addition of time to a sentence I never deserved... but you imposed.","From end to end of this galaxy, all are blind!","Side by side we march as one. Humans and Elites will die. The Earth will fall if we strike together. And so forth shall all of life.","All life dies, all worlds too, and if there is guaranteed perpetual existence, after that - what does it matter how the end comes?","All consumption is death for the consumed. Yet all must eat, so we all bring damnation to one creature or another."'''
t = text.strip('"').split('","')
print(t)
Output:
['This is not your grave... but you are welcome in it.',
'I am a monument to all your sins.',
'There is much talk, and I have listened, through rock and metal and time. Now I shall talk, and you shall listen.',
... snipp ...,
'All life dies, all worlds too, and if there is guaranteed perpetual existence, after that - what does it matter how the end comes?',
'All consumption is death for the consumed. Yet all must eat, so we all bring damnation to one creature or another.']
Upvotes: 1
Reputation: 2365
If you have control over the input, you can just eval
your string.
>>> lines = '''"This is not your grave... but you are welcome in it.","I am a monument to all your sins.","There is much talk, and I have listened, through rock and metal and time. Now I shall talk, and you shall listen.","If you will not hear the truth, then I will show it to you.","Arrogant creatures! Your deaths will be instantaneous, while we shall suffer the progress of infinitude!","We exist together now, many corpses in one grave...","Silence fills the empty grave, now that I have gone. But my mind is not at rest, for questions linger on. I will ask, and you will answer.","Do not be afraid. I am peace; I am salvation.","I am a timeless chorus. Join your voice with mine, and sing victory everlasting.","Do not shoot, but listen! Let me lead you safely... to our foe. Only you can halt... what he has set in motion.","Lies for the weak! Beacons for the deluded!","You will be food; nothing more...","Now the gate has been unlatched, headstones pushed aside; corpses shift and offer room, a fate you must abide!","Child of my enemy, why have you come? I offer no forgiveness, a father's sins, passed to his son.","Of course, you came for her. We exist together now, two corpses in one grave.","And yet, perhaps a part of her remains...","Time... has taught me patience! But basking in new freedom, I will know all that I possess!","Submit! End her torment and my own!","You will show me what she hides! Or I shall feast upon your BONES!","I have consumed a galaxy of flesh and mind and bone!","Do I take life or give it? Who is victim, and who is foe?","Resignation is my virtue; like water I ebb, and flow. Defeat is simply the addition of time to a sentence I never deserved... but you imposed.","From end to end of this galaxy, all are blind!","Side by side we march as one. Humans and Elites will die. The Earth will fall if we strike together. And so forth shall all of life.","All life dies, all worlds too, and if there is guaranteed perpetual existence, after that - what does it matter how the end comes?","All consumption is death for the consumed. Yet all must eat, so we all bring damnation to one creature or another."'''
>>> result = eval(lines)
>>> len(result)
26
>>> result[0]
'This is not your grave... but you are welcome in it.'
Upvotes: 1