0
点赞
收藏
分享

微信扫一扫

re.sub(‘[^A-Za-z]+‘, ‘ ‘, line).strip().lower()


re.sub('[^A-Za-z]+', ' ', line).strip().lower()

^ :代表非

^A-Za-z: 代表非字母

[^A-Za-z]+ :可连续多个非字母的字符

re.sub('[^A-Za-z]+', ' ', line):将line字符串中的 连续多个非字母的字符 变成 空格

.strip() :去掉首位的空格

.lower():把大写字母全部统一成小写

示例:

re.sub('[^A-Za-z]+', ' ', '\n')
Out[5]: ' '
re.sub('[^A-Za-z]+', ' ', 'I\n')
Out[6]: 'I '
re.sub('[^A-Za-z]+', ' ', 'The')
Out[7]: 'The'
re.sub('[^A-Za-z]+', ' ', 'The ')
Out[8]: 'The '
re.sub('[^A-Za-z]+', ' ', 'The Time Machine, by H. G. Wells [1898]\n')
Out[9]: 'The Time Machine by H G Wells '
re.sub('[^A-Za-z]+', ' ', 'The Time Machine, by H. G. Wells [1898]\n').strip().lower()
Out[10]: 'the time machine by h g wells'

举报

相关推荐

python re.sub

关于python 的re.sub用法

0 条评论