删除列表项中的引号 [英] Removing quotation marks from list items

查看:210
本文介绍了删除列表项中的引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行该程序:

f = open( "animals.txt", "r")
g = f.read()
g1 = g.split(',') #turning the file into list
print g1

我希望这个出来:

['ELDEN', 'DORSEY', 'DARELL', 'BRODERICK', 'ALONSO']

相反,我得到这个:

['"ELDEN"', '"DORSEY"', '"DARELL"', '"BRODERICK"', '"ALONSO"']

有人知道如何删除内引号

Does anyone know how to remove the inner quotes

推荐答案

尝试使用列表理解,您可以有效地将列表重新分配为等效的列表,并删除引号.

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

这篇关于删除列表项中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆