如何在python中替换列表中的单引号 [英] How to replace single quotes from a list in python

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

问题描述

我有一个列表:

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]

推荐答案

您的列表不包含任何带有单引号的字符串.我认为您正在将字符串的repr()表示与其值混淆.

Your list doesn't contain any strings with single quotes. I think you are confusing the repr() representation of the strings with their values.

当您打印Python标准库容器(例如列表(或元组,集合,字典等)时,会显示该容器的 contents repr()表示输出;这在调试时非常有用,因为它可以清楚说明您拥有的对象类型.对于字符串,表示形式使用有效的Python字符串文字语法;您可以复制输出并将其粘贴到另一个Python脚本或交互式解释器中,然后获得完全相同的值.

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; 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; you can copy the output and paste it into another Python script or the interactive interpreter and you'll get the exact same value.

例如,s这是一个字符串,其中包含一些文本,一些引号字符和换行符.当我 print 字符串时,换行符会导致额外的空白行被打印,但是当我使用repr()时,您将获得Python语法形式的字符串值,其中单引号是语法,而不是值.请注意,换行符也以\n语法显示,与我首先创建s字符串时完全相同:

For example, s here is a string that contains some text, some quote characters, and a newline character. 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. 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:

>>> 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'

当我在最后回显s值时,交互式解释器还会使用repr()输出向我显示该值.

And when I echoed the s value at the end, the interactive interpreter also shows me the value using the repr() output.

因此,在您的列表中,字符串不包含'字符作为值的一部分.它们是字符串语法的一部分.您只需要替换"字符,它们就成为值的一部分 ,因为它们位于最外层的'...'字符串文字语法内.您可以使用str.replace('"', '')删除它们:

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:

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

或者,您可以使用 str.strip()方法仅删除值开头或结尾的引号:

or, you could use the str.strip() method to only remove quotes that are at the start or end of the value:

[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

但是,我已经看到您正在从您手工分析的制表符分隔的文件中读取数据.如果改用引号> csv.reader()对象,配置为将制表符作为分隔符.该类自动将处理带引号的列:

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:

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']

演示csv.reader()如何处理引号的演示:

Demo showing how csv.reader() handles quotes:

>>> 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']

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

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