【问题标题】:Is stripping string by '\r\n ' necessary in Python?在 Python 中是否需要通过 '\r\n' 剥离字符串?
【发布时间】:2019-01-03 06:27:26
【问题描述】:

在 Java 中,必须使用 \r\n 进行剥离,例如split( "\r\n") is not splitting my string in java

但是\r\n 在 Python 中是必需的吗?以下是真的吗?

str.strip() == str.strip('\r\n ')

来自docs

返回带有前导和尾随字符的字符串的副本 删除。 chars 参数是指定集合的​​字符串 要删除的字符。如果省略或无,chars 参数 默认删除空格。 chars 参数不是前缀或 后缀;相反,它的值的所有组合都被剥离了

从这个CPython teststr.strip()看来是在剥离:

 \t\n\r\f\v

谁能指出我在 CPython 中执行字符串剥离的代码?

【问题讨论】:

  • 除非您想保留其他空白字符,否则没有必要。

标签: python string whitespace strip cpython


【解决方案1】:

你在寻找这些台词吗?

https://github.com/python/cpython/blob/e42b705188271da108de42b55d9344642170aa2b/Objects/unicodeobject.c#L12222-L12247

#define LEFTSTRIP 0
#define RIGHTSTRIP 1
#define BOTHSTRIP 2

/* Arrays indexed by above */
static const char *stripfuncnames[] = {"lstrip", "rstrip", "strip"};

#define STRIPNAME(i) (stripfuncnames[i])

/* externally visible for str.strip(unicode) */
PyObject *
_PyUnicode_XStrip(PyObject *self, int striptype, PyObject *sepobj)
{
    void *data;
    int kind;
    Py_ssize_t i, j, len;
    BLOOM_MASK sepmask;
    Py_ssize_t seplen;

    if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1)
        return NULL;

    kind = PyUnicode_KIND(self);
    data = PyUnicode_DATA(self);
    len = PyUnicode_GET_LENGTH(self);
    seplen = PyUnicode_GET_LENGTH(sepobj);
    sepmask = make_bloom_mask(PyUnicode_KIND(sepobj),
                              PyUnicode_DATA(sepobj),
                              seplen);

    i = 0;
    if (striptype != RIGHTSTRIP) {
        while (i < len) {
            Py_UCS4 ch = PyUnicode_READ(kind, data, i);
            if (!BLOOM(sepmask, ch))
                break;
            if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0)
                break;
            i++;
        }
    }

    j = len;
    if (striptype != LEFTSTRIP) {
        j--;
        while (j >= i) {
            Py_UCS4 ch = PyUnicode_READ(kind, data, j);
            if (!BLOOM(sepmask, ch))
                break;
            if (PyUnicode_FindChar(sepobj, ch, 0, seplen, 1) < 0)
                break;
            j--;
        }

        j++;
    }

    return PyUnicode_Substring(self, i, j);
}

【讨论】:

    【解决方案2】:

    基本上:

    str.strip() == str.strip(string.whitespace) == str.strip(' \t\n\r\f\v') != str.strip('\r\n')
    

    除非您明确尝试仅删除换行符,否则 str.strip()str.strip('\r\n') 是不同的。

    >>> '\nfoo\n'.strip()
    'foo'
    >>> '\nfoo\n'.strip('\r\n')
    'foo'
    >>> '\r\n\r\n\r\nfoo\r\n\r\n\r\n'.strip()
    'foo'
    >>> '\r\n\r\n\r\nfoo\r\n\r\n\r\n'.strip('\r\n')
    'foo'
    >>> '\n\tfoo\t\n'.strip()
    'foo'
    >>> '\n\tfoo\t\n'.strip('\r\n')
    '\tfoo\t'
    

    这一切看起来都很好,但请注意,如果换行符和字符串的开头或结尾之间有空格(或任何其他字符),.strip('\r\n') 不会删除换行符。

    >>> '\t\nfoo\n\t'.strip()
    'foo'
    >>> '\t\nfoo\n\t'.strip('\r\n')
    '\t\nfoo\n\t'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 2017-06-08
      • 1970-01-01
      • 2011-05-02
      相关资源
      最近更新 更多