Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 子类字符串中的Santizing输入无法按预期工作_String_Python 2.7_Replace - Fatal编程技术网

String 子类字符串中的Santizing输入无法按预期工作

String 子类字符串中的Santizing输入无法按预期工作,string,python-2.7,replace,String,Python 2.7,Replace,我有一个用例,我想清理作为str类输入的字符串。换句话说,删除字符串中的控制字符 我试过这个 [hamartin@Guvny bin]$ ipython Python 2.7.12 (default, Sep 29 2016, 13:30:34) Type "copyright", "credits" or "license" for more information. IPython 3.2.1 -- An enhanced Interactive Python. ? -&

我有一个用例,我想清理作为str类输入的字符串。换句话说,删除字符串中的控制字符

我试过这个

[hamartin@Guvny bin]$ ipython
Python 2.7.12 (default, Sep 29 2016, 13:30:34) 
Type "copyright", "credits" or "license" for more information.

IPython 3.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: class LogString(str):
   ...:     def __init__(self, msg, *args, **kwargs):
   ...:         nstr = msg.replace('\xc2', '')
   ...:         nstr = nstr.replace('\xa0', ' ')
   ...:         super(LogString, self).__init__(nstr, *args, **kwargs)
   ...:         

In [2]: repr(LogString('Testing this out'))
Out[2]: "'Testing\\xc2\\xa0this\\xc2\\xa0out'"
我知道这种特殊情况下的更换工作

[hamartin@Guvny bin]$ ipython
Python 2.7.12 (default, Sep 29 2016, 13:30:34) 
Type "copyright", "credits" or "license" for more information.

IPython 3.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: i = 'Testing this out'

In [2]: repr(i)
Out[2]: "'Testing\\xc2\\xa0this\\xc2\\xa0out'"

In [3]: i = i.replace('\xc2', '')

In [4]: repr(i.replace('\xa0', ' '))
Out[4]: "'Testing this out'"

In [5]:

我不会将原始字符串存储在任何地方,除非存储在临时变量中。我正在替换字符,然后再将其传递到树上。为什么创建的对象中包含原始字符串而不是“已清理”字符串?

Python中的字符串是不可变的。由于您正在子类化
str
,因此一旦提供了值,就无法更改该值。相反,请重写
\uuuu new\uuuu
静态方法:

class LogString(str):
    def __new__(cls, msg):
        nstr = msg.replace('\xc2', '')
        nstr = nstr.replace('\xa0', ' ')
        return str.__new__(cls, nstr)

希望这有帮助

我测试了一下,它成功了。我在News上找到的文档也证实了你所说的。我仍然不明白为什么我的例子不起作用。新方法是在init msg之前执行的,并且从那一刻起msg是不可变的吗?是什么原因让str的新方法无法运行?如果我所写的是正确的,str的新方法是否有任何有用的功能,我可能想在我的方法中复制?@Mogget没错,
\uuuuuuuuuuuuuuuuuuuuuuuuuuuu
方法甚至在创建对象之前就已经执行,而
\uuuuuuuuuuuuuuu init\uuuuu
实际上是执行的实例的第一个函数。这就是为什么前者是静态方法,在创建对象之前更改字符串。不确定你对最后一个问题的意思是什么,你在最后一行调用原始str的
\uuuu new\uuuu
方法!