简体   繁体   English

如何在python中替换列表中的单引号

[英]How to replace single quotes from a list in python

I have a list: 我有一个清单:

my_list = ['"3"', '"45"','"12"','"6"']

This list has single and double quotes and the item value. 该列表具有单引号和双引号以及项目值。 How can I replace either the single or double quotes from each item. 如何替换每个项目中的单引号或双引号。 I tried below, but the results are same: 我在下面尝试过,但是结果是一样的:

my_list = [i.replace("''", " ") for i in my_list]

Your list doesn't contain any strings with single quotes. 您的列表不包含任何带有单引号的字符串。 I think you are confusing the repr() representation of the strings with their values. 我认为您正在将字符串的repr()表示与它们的值混淆。

When you print a Python standard library container such as a list (or a tuple, set, dictionary, etc.) then the contents of such a container are shown their repr() representation output; 当您打印Python标准库容器(例如列表(或元组,集合,字典等))时,该容器的内容将显示为它们的repr()表示输出。 this is great when debugging because it makes it clear what type of objects you have. 这在调试时非常有用,因为它可以清楚说明您拥有的对象类型。 For strings, the representation uses valid Python string literal syntax; 对于字符串,表示形式使用有效的Python字符串文字语法; you can copy the output and paste it into another Python script or the interactive interpreter and you'll get the exact same value. 您可以复制输出并将其粘贴到另一个Python脚本或交互式解释器中,并获得完全相同的值。

For example, s here is a string that contains some text, some quote characters, and a newline character. 例如,这里的s是一个包含一些文本,一些引号字符和换行符的字符串。 When I print the string, the newline character causes an extra blank line to be printed, but when I use repr() , you get the string value in Python syntax form, where the single quotes are part of the syntax , not the value. 当我打印字符串时,换行符会导致额外的空白行被打印,但是当我使用repr() ,您将以Python语法形式获得字符串值,其中单引号是语法的一部分,而不是值。 Note that the newline character also is shown with the \\n syntax, exactly the same as when I created the s string in the first place: 请注意,换行符也使用\\n语法显示,与我首先创建s字符串时完全相同:

>>> s = 'They heard him say "Hello world!".\n'
>>> print(s)
They heard him say "Hello world!".

>>> print(repr(s))
'They heard him say "Hello world!".\n'
>>> s
'They heard him say "Hello world!".\n'

And when I echoed the s value at the end, the interactive interpreter also shows me the value using the repr() output. 当我在末尾回显s值时,交互式解释器还会使用repr()输出向我显示该值。

So in your list, your strings do not have the ' characters as part of the value. 因此,在列表中,字符串不包含'字符作为值的一部分。 They are part of the string syntax. 它们是字符串语法的一部分。 You only need to replace the " characters, they are part of the value, because they are inside the outermost '...' string literal syntax. You could use str.replace('"', '') to remove them: 您只需要替换"字符,它们成为值的一部分,因为它们位于最外层的'...'字符串文字语法中。您可以使用str.replace('"', '')删除它们:

[value.replace('"', '') for value in my_list]

or, you could use the str.strip() method to only remove quotes that are at the start or end of the value: 或者,您可以使用str.strip()方法仅删除值开头或结尾的引号:

[value.strip('"') for value in my_list]

Both work just fine for your sample list: 两者都适合您的样本列表:

>>> my_list = ['"3"', '"45"','"12"','"6"']
>>> [value.replace('"', '') for value in my_list]
['3', '45', '12', '6']
>>> [value.strip('"') for value in my_list]
['3', '45', '12', '6']

Again, the ' characters are not part of the value: 同样, '字符不是值的一部分:

>>> first = my_list[0].strip('"')
>>> first         # echo, uses repr()
'3'
>>> print(first)  # printing, the actual value written out
3
>>> len(first)    # there is just a single character in the string
1

However , I have seen that you are reading your data from a tab-separated file that you hand-parse. 但是 ,我已经看到您正在从手工分析的制表符分隔的文件中读取数据。 You can avoid having to deal with the " quotes altogether if you instead used the csv.reader() object , configured to handle tabs as the delimiter. That class automatically will handle quoted columns: 您可不必处理"的报价完全如果你代替csv.reader()对象 ,配置为处理选项卡作为分隔符那类会自动将处理引用列:

import csv

with open(inputfile, 'r', newline='') as datafile:
    reader = csv.reader(datafile, delimiter='\t')
    for row in reader:
        # row is a list with strings, *but no quotes*
        # e.g. ['3', '45', '12', '6']

Demo showing how csv.reader() handles quotes: 该演示显示了csv.reader()如何处理引号:

>>> import csv
>>> lines = '''\
... "3"\t"45"\t"12"\t"6"
... "42"\t"81"\t"99"\t"11"
... '''.splitlines()
>>> reader = csv.reader(lines, delimiter='\t')
>>> for row in reader:
...     print(row)
...
['3', '45', '12', '6']
['42', '81', '99', '11']

As suggested by @MartijnPieters in comments, you can use replace on the strings to get the desired output. 正如@MartijnPieters在评论中建议的那样,您可以在字符串上使用replace以获得所需的输出。

The change I like to suggest is that using .replace('"', '') instead of .replace('"', ' ') . 我想建议的更改是使用.replace('"', '')而不是.replace('"', ' ') Otherwise the resultant strings will have a leading and trailing white space 否则,结果字符串将具有前导和尾随空格

You can use list comprehension to deal with the list you have like this 您可以使用列表理解来处理这样的列表

my_list = ['"3"', '"45"','"12"','"6"']

new_list = [x.replace('"', '') for x in my_list]

print(new_list) # ['3', '45', '12', '6']

You can use split: 您可以使用split:

[x.split('"')[1] for x in my_list]

or you can use: 或者您可以使用:

[x.strip('"') for x in my_list]

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

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