仅在双引号上分解文本的长段引号



我见过这个:Python 解析 CSV 忽略带双引号的逗号

许多人喜欢它。但它对我的文本没有任何作用。我已经把它放在一个列表中,我需要根据双引号外的逗号来打破它。

下面是一些示例文本和我的代码:

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的列表。

如果你可以控制输入,你可以只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.'

条带开始和结束",在'","'处拆分:

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.']

最新更新