简体   繁体   English

去除python中的引号

[英]Removing quotation marks in python

I used python to import txt data as list.我使用python将txt数据作为列表导入。 As a result, there are double quotes on the items in the list which i do not want.结果,我不想要的列表中的项目上有双引号。 I want to remove them but I'm currently having some difficulties.我想删除它们,但我目前遇到了一些困难。 I would be glad if someone can kindly help me with this issue.如果有人能帮助我解决这个问题,我会很高兴。

The list in python is of this nature: python中的列表具有这种性质:

lst = ["'BEN','JIM'", "'GIN','ANN'"]

I want to have the double quotes removed so I can get this list:我想删除双引号,这样我就可以得到这个列表:

lst = ['BEN','JIM', 'GIN','ANN']

I tried this lines of code but to no avail:我尝试了这行代码,但无济于事:

lst = ["'BEN','JIM'", "'GIN','ANN'"]
lst = [x.strip("'") for x in lst] 
lst

and the result is not what im expecting:结果不是我所期望的:

["BEN','JIM", "GIN','ANN"]

Again, I would be grateful for your help.再次感谢您的帮助。

You have confused the display representation of an item as being equivalent to its value.您已将项目的显示表示混淆为等同于其值。 Look at what you have: a list of two elements:看看你有什么:两个元素的列表:

["'BEN','JIM'", 
 "'GIN','ANN'"]

You want to obtain a list of four elements:您想要获取包含四个元素的列表:

['BEN',
'JIM',
'GIN',
'ANN']

You cannot do this by simple character manipulation: the operations you tried do not change the quantity of elements.你不能通过简单的字符操作来做到这一点:你尝试的操作不会改变元素的数量。

Instead, you have to process the elements you have, splitting 2-for-1.相反,您必须处理您拥有的元素,将 2-for-1 拆分。 I'll keep the Python technology low ...我将保持 Python 技术的低...

new_lst = []
for two_name in lst:
    name_pair = two_name.split(',')
    new_lst.extend(name_pair)

Output:输出:

["'BEN'", "'JIM'", "'GIN'", "'ANN'"]

Now you can use your previous technique to remove the single-quotes, leaving you with the four, 3-letter names you wanted.现在您可以使用之前的技术删除单引号,留下您想要的四个 3 个字母的名称。

Does that solve your problem?这能解决你的问题吗?

Use the replace() string function to get rid of the single quotes, and then split on commas.使用replace()字符串函数去掉单引号,然后用逗号分割。

lst = ["'BEN','JIM'", "'GIN','ANN'"]
newlst = []
for pair in lst:
    for name in pair.replace("'", "").split(","):
        newlst.append(name)

The data looks similar to a CSV file where the individual cells are quoted.数据看起来类似于引用单个单元格的 CSV 文件。 If it looks like a duck... use a reader to strip them for you.如果它看起来像一只鸭子……请使用阅读器为您剥离它们。 This is a nested list comprehension to first build rows from the the list and then to flatten them to a single list.这是一个嵌套列表理解,首先从列表中构建行,然后将它们展平为单个列表。

>>> import csv
>>> lst = ["'BEN','JIM'", "'GIN','ANN'"]
>>> [cell for row in csv.reader(lst, quotechar="'") for cell in row]
['BEN', 'JIM', 'GIN', 'ANN']
lst = ["'BEN','JIM'", "'GIN','ANN'"]
lst = "".join(lst)
print(lst)

Output:输出:

'BEN','JIM''GIN','ANN'

You can do thi with list comprehension on two lines:你可以用两行的列表理解来做到这一点:

input:输入:

lst = ["'BEN','JIM'", "'GIN','ANN'"]

lst1 = [x.replace("'", "").split(',') for x in lst]
lst2 = [z for y in lst1 for z in y]

or on one line或在一行上

lst2 = [z for y in [x.replace("'", "").split(',') for x in lst] for z in y]

output:输出:

['BEN', 'JIM', 'GIN', 'ANN']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM