【问题标题】:Break up long segment of quotes of text only on double quotes?仅在双引号上分解长段文本引号?
【发布时间】:2019-08-27 16:55:41
【问题描述】:

我看过这个: 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 的列表。

【问题讨论】:

    标签: python python-3.x string split


    【解决方案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.']
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2019-12-17
        • 2019-07-05
        • 2018-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        相关资源
        最近更新 更多