Python 从列表项中删除引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28376538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing quotation marks from list items
提问by David Washington
I am running this program:
我正在运行这个程序:
f = open( "animals.txt", "r")
g = f.read()
g1 = g.split(',') #turning the file into list
print g1
And I want this to come out:
我希望这个出来:
['ELDEN', 'DORSEY', 'DARELL', 'BRODERICK', 'ALONSO']
Instead, I am getting this:
相反,我得到了这个:
['"ELDEN"', '"DORSEY"', '"DARELL"', '"BRODERICK"', '"ALONSO"']
Does anyone know how to remove the inner quotes
有谁知道如何删除内部引号
采纳答案by Malik Brahimi
Try a list comprehension which allows you to effectively and efficiently reassign the list to an equivalent list with quotes removed.
尝试列表理解,它允许您有效且高效地将列表重新分配给删除引号的等效列表。
g1 = [i.replace('"', '') for i in g1] # remove quote from each element
回答by Malik Brahimi
You can easily strip off the quote characters using a list comprehensionand str.strip
:
您可以使用列表理解轻松去除引号字符,并且str.strip
:
f = open( "animals.txt", "r")
g = f.read()
g1 = [x.strip('"') for x in g.split(',')]
print g1
Demo:
演示:
>>> lst = ['"ELDEN"', '"DORSEY"', '"DARELL"', '"BRODERICK"', '"ALONSO"']
>>> [x.strip('"') for x in lst]
['ELDEN', 'DORSEY', 'DARELL', 'BRODERICK', 'ALONSO']
>>>
回答by Hugh Bothwell
Maybe we should go back to the source.
也许我们应该回到源头。
Apparently your file contains the text "ELDEN", "DORSEY", "DARELL", "BRODERICK", "ALONSO"
(etc). We can delegate the parsing:
显然您的文件包含文本"ELDEN", "DORSEY", "DARELL", "BRODERICK", "ALONSO"
(等)。我们可以委托解析:
import ast
with open("animals.txt") as inf:
g1 = ast.literal_eval("[" + inf.read() + "]")
回答by John Y
What is creating your file in the first place? It looks as though you have a kind of CSV where either all elements, or at least all string elements, are surrounded by quotation marks. As such, you have at least a couple of choices for moving the quotation-mark removal "upstream" from what most of the other answers here are suggesting:
首先是什么创建了您的文件?看起来好像您有一种 CSV,其中所有元素或至少所有字符串元素都用引号括起来。因此,您至少有几个选择可以将引号删除从这里的大多数其他答案建议的“上游”移动:
- Use the
csv
module:
- 使用
csv
模块:
import csv with open('animals.txt', 'r') as f: g = csv.reader(f) g1 = g.next()
import csv with open('animals.txt', 'r') as f: g = csv.reader(f) g1 = g.next()
- Include the quotation marks in your
split()
, and chop off the leading and trailing quotes first:
- 在你的
split()
, 中包含引号,并首先去掉前导和尾随引号:
with open('animals.txt', 'r') as f: g = f.read() g1 = g[1:-1].split('","')
with open('animals.txt', 'r') as f: g = f.read() g1 = g[1:-1].split('","')
The first option may be somewhat more "heavyweight" than what you're looking for, but it's more robust (and still not thatheavy). Note that the second option assumes that your file does NOT have a trailing newline at the end. If it does, you have to adjust accordingly.
第一个选项可能比您正在寻找的更“重量级”,但它更强大(但仍然没有那么重)。请注意,第二个选项假定您的文件末尾没有尾随换行符。如果是这样,您必须相应地进行调整。