如何从python列表中的字符串中删除双引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24123705/
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
How to remove double quotes from strings in a list in python?
提问by EerlijkeDame
I am trying to get some data in a list of dictionaries. The data comes from a csv file so it's all string. the the keys in the file all have double qoutes, but since these are all strings, I want to remove them so they look like this in the dictionary:
我试图在字典列表中获取一些数据。数据来自一个 csv 文件,所以它都是字符串。文件中的键都有双引号,但由于这些都是字符串,我想删除它们,使它们在字典中看起来像这样:
{'key':value}
instead of this
而不是这个
{'"key"':value}
I tried simply using string = string[1:-1], but this doesn's work...
我尝试简单地使用 string = string[1:-1],但这不起作用......
Here is my code:
这是我的代码:
csvDelimiter = ","
tsvDelimiter = "\t"
dataOutput = []
dataFile = open("browser-ww-monthly-201305-201405.csv","r")
for line in dataFile:
line = line[:-1] # Removes \n after every line
data = line.split(csvDelimiter)
for i in data:
if type(i) == str: # Doesn't work, I also tried if isinstance(i, str)
# but that didn't work either.
print i
i = i[1:-1]
print i
dataOutput.append({data[0] : data[1]})
dataFile.close()
print "Data output:\n"
print dataOutput
all the prints I get from print i are good, without double quotes, but when I append data to dataOutput, the quotes are back!
我从 print i 获得的所有打印都很好,没有双引号,但是当我将数据附加到 dataOutput 时,引号又回来了!
Any idea how to make them disappear forever?
知道如何让它们永远消失吗?
采纳答案by Iguananaut
As noted in the comments, when dealing with CSV files you truly ought to use Python's built-in csv module(linking to Python 2 docs since it seems that's what you're using).
如评论中所述,在处理 CSV 文件时,您确实应该使用 Python 的内置csv 模块(链接到 Python 2 文档,因为这似乎是您正在使用的)。
Another thing to note is that when you do:
另一件需要注意的是,当你这样做时:
data = line.split(csvDelimiter)
data = line.split(csvDelimiter)
every item in the returned list, if it is not empty, will be strings. There's no sense in doing a type check in the loop (though if there were a reason to you would use isinstance
). I don't know what "didn't work" about it, though it's possible you were using unicode strings. On Python 2 you can usually use isinstance(..., basestring)
where basestring
is a base class for both str
and unicode
. On Python 3 just use str
unless you know you're dealing with bytes
.
返回列表中的每个项目,如果它不为空,将是字符串。在循环中进行类型检查是没有意义的(尽管如果有理由你会使用isinstance
)。我不知道什么“不起作用”,尽管您可能正在使用 unicode 字符串。在Python 2中,通常可以使用isinstance(..., basestring)
其中basestring
的两个基类str
和unicode
。在 Python 3 上,str
除非你知道你正在处理bytes
.
回答by Lennart Regebro
Strip it. For example:
剥它。例如:
data[0].strip('"')
However, when reading cvs files, the best is to use the built-in cvs
module. It takes care of this for you.
但是,在读取cvs文件时,最好使用内置cvs
模块。它会为您解决这个问题。
回答by msch
You said: "I tried simply using string = string[1:-1], but this doesn't work...". It seems to work fine for me:
你说:“我试着简单地使用 string = string[1:-1],但这不起作用......”。对我来说似乎工作正常:
In [101]: s="'word'"
In [102]: s[1:-1]
Out[102]: 'word'