强制 repr() 使用单引号

Posted

技术标签:

【中文标题】强制 repr() 使用单引号【英文标题】:Force repr() to use single quotes 【发布时间】:2015-02-08 17:11:05 【问题描述】:

我有一个问题,有没有办法“强制”repr() 在字符串周围始终创建单引号?

当我只使用repr()时会发生这种情况

print repr("test")
'test'
print repr("test'")
"test'"
print repr("test\"")
'test"'
print repr("test'\"")
'test\'"'

所以最后一个实际上是我想要的,但我不想总是添加 \\" 来获得单引号。


编辑:我不会将答案标记为已接受,因为正如@martijn-pieters 所指出的,我将repr() 用于它不打算用于的目的。

【问题讨论】:

也许this 会帮助你 repr() 是一个调试工具。为什么您要求它使用不同的引号?作为查看输出的开发人员,您可以很好地区分报价类型。当您粘贴回repr(stringobject) 调用的结果时,Python 也可以;您可以通过这种方式重新创建确切的值。这些是repr() 的用例。但很明显,您正在将其用于其他用途。几乎可以肯定,最好以不同的方式解决问题,使用repr() 我尝试使用repr() 在文件中写入列表,然后添加换行符和制表符以在文本文件中对其进行格式化。最后,我想要另一个函数,它使用ast.iteral_eval() 从文件中的字符串(不添加换行符和制表符)创建列表。 【参考方案1】:

好吧,如果你的对象总是一个字符串,你可以这样做:

def repr_single(s):
    return "'" + repr('"' + s)[2:]

print repr_single("test'")
'test\''

但是正如 Martijn Pieters 所问的那样,我很好奇您在这里的用例。

【讨论】:

看起来不错,但是我注意到我不一定需要repr()。我的目标是用所有特殊字符转义一个字符串【参考方案2】:

我需要做一次类似的事情,除了我希望它总是“更喜欢”使用双引号——这意味着使用它们,除非字符串中的双引号多于单引号(为了尽量减少它们的数量需要转义)。

我这样做的方法是继承内置的str 类并覆盖它的__repr__() 方法。您可能很容易将其中的逻辑颠倒过来做相反的事情(以及强制使用的角色始终是一个或另一个)。

FWIW,这是代码:

# -*- coding: iso-8859-1 -*-

# Special string subclass to override the default
# representation method. Main purpose is to
# prefer using double quotes and avoid hex
# representation on chars with an ord() > 128
class MsgStr(str):
    def __repr__(self):
        # use double quotes unless there are more of them in the string than
        # single quotes
        quotechar = '"' if self.count("'") >= self.count('"') else "'"
        rep = [quotechar]
        for ch in self:
            # control char?
            if ord(ch) < ord(' '):
                # remove the single quotes around the escaped representation
                rep += repr(str(ch)).strip("'")
            # does embedded quote match quotechar being used?
            elif ch == quotechar:
                rep += "\\"
                rep += ch
            # else just use others as they are
            else:
                rep += ch
        rep += quotechar

        return "".join(rep)

if __name__ == "__main__":
    s1 = '\tWürttemberg'
    s2 = MsgStr(s1)
    print "str    s1:", s1
    print "MsgStr s2:", s2
    print "--only the next two should differ--"
    print "repr(s1):", repr(s1), "# uses built-in string 'repr'"
    print "repr(s2):", repr(s2), "# uses custom MsgStr 'repr'"
    print "str(s1):", str(s1)
    print "str(s2):", str(s2)
    print "repr(str(s1)):", repr(str(s1))
    print "repr(str(s2)):", repr(str(s2))
    print "MsgStr(repr(MsgStr('\tWürttemberg'))):", MsgStr(repr(MsgStr('\tWürttemberg')))
    assert eval(MsgStr(repr(MsgStr('\tWürttemberg')))) == MsgStr('\tWürttemberg')

输出:

str    s1:  Württemberg
MsgStr s2:  Württemberg
--only the next two should differ--
repr(s1): '\tW\xfcrttemberg' # uses built-in string 'repr'
repr(s2): "\tWürttemberg" # uses custom MsgStr 'repr'
str(s1):    Württemberg
str(s2):    Württemberg
repr(str(s1)): '\tW\xfcrttemberg'
repr(str(s2)): '\tW\xfcrttemberg'
MsgStr(repr(MsgStr('    Württemberg'))): "\tWürttemberg"

【讨论】:

我以不同的方式解决了这个问题,因为我注意到我只需要转义一些字符,但还是谢谢你【参考方案3】:

我继续使用 repr_double 实现了 标准输出的repr_single

def repr_single(s):
    return "'" + repr('"' + s)[2:]

def repr_double(s):
    single = repr_single(s)
    return '"' + single[1:-1].replace('"', '\\"').replace('\\\'', '\'') + '"'

def test_single():
    assert r"'foobar'" == repr_single('foobar')
    assert r"'\'foobar'" == repr_single('\'foobar')
    assert "'\\'foobar'" == repr_single("'foobar")

def test_double():
    assert r'"foobar"' == repr_double("foobar")
    assert '"\'foobar"' == repr_double("'foobar")
    assert '"\\"foobar"' == repr_double('"foobar')

【讨论】:

以上是关于强制 repr() 使用单引号的主要内容,如果未能解决你的问题,请参考以下文章

python打印日志(logging)字典是单引号,转json时不识别单引号,怎么能打印出来时双引号

DAO.Recordset MySQL 后端在字符串周围强制使用单引号

了解双引号与单引号的字符串生成

( # #@ ## 在define中的应用)或( 连接两个字符串或者两个数字强制转化成单引号强制转化成双引号 )附加字符串强制转化成数字

get参数存在+号 双引号、多引号问题

json反序列化的时候字符串为单引号的一个坑