作者:_郭士铭 | 来源:互联网 | 2024-09-27 17:49
我正在研究scrapy项目,并试图解析我的配置字符串是attr_title我必须去掉’attr_’并获得标题.我使用了lstrip(‘attr_’),但得到了意想不到的结果.我知道
我正在研究scrapy项目,并试图解析我的配置
字符串是attr_title我必须去掉’attr_’并获得标题.我使用了lstrip(‘attr_’),但得到了意想不到的结果.我知道lstrip可以解决这些问题并删除它们,但很难理解它.
In [17]: "attr.title".lstrip('attr.')
Out[17]: 'itle'
PS:我知道有多种提取字符串的解决方案我有兴趣了解这一点.
解决方法:
lstrip遍历结果字符串,直到没有更多的组合与最左边的字符集匹配
下面有一个小插图.
In [1]: "attr.title".lstrip('attr.')
Out[1]: 'itle' # Flow --> "attr." --> "t" --> Next char is 'i' which does not match any combination hence, iteration stops & end result ('itle') is returned
In [2]: "attr.tritle".lstrip('attr.')
Out[2]: 'itle' # "attr." --> "t" --> "r" --> Next char is 'i' which does not match any combination hence, iteration stops & end result ('itle') is returned
In [5]: "attr.itratitle".lstrip('attr.')
Out[5]: 'itratitle' # "attr." --> Next char is 'i' which does not match any combination hence, iteration stops & end result ('itratitle') is returned