Home  >  Q&A  >  body text

python - Scrapy正则表达式怎么去掉空格和换行符?

伊谢尔伦伊谢尔伦2763 days ago1395

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:09:54

    You can use the re.sub function to perform regular replacement,
    sub(pattern, repl, string, count=0)

    • First parameter: Rule

    • The second parameter: the replaced string

    • The third parameter: string

    • The fourth parameter: the number of replacements. Defaults to 0, meaning every match is replaced

    >>> a="\t\t\n 123"
    >>> a
    '\t\t\n 123'
    >>> b = re.sub(r'\s+','', a) #\s   匹配任意的空白符
    >>> b
    '123'

    reply
    0
  • Cancelreply