我已经看到了: Python parse CSV ignoring comma with double-quotes
许多人喜欢它。但这对我的文字没有任何作用。我已经将它放在一个列表中,我需要根据双引号之外的逗号将其打断。
这是一些示例文本和我的代码:
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)
但是,这样做的最终结果是我有一个长度为1的列表,这对我没有任何好处。
我真的不知道如何获得它,所以每个句子都变成它自己在列表中的条目。我确定这是重复的,但是我已经寻找了一段时间,找不到任何人遇到这个问题,因为它变成了长度为1的列表。
答案 0 :(得分:1)
开始和结束"
的条带,在'","'
处分开:
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)
输出:
['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.']
答案 1 :(得分:1)
如果您可以控制输入,则只需eval
您的字符串即可。
>>> 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.'