秘籍总结:玩转python里的字符串上篇
来源:互联网 发布:贵州广电网络节目表 编辑:程序博客网 时间:2024/06/08 01:06
tips
re 别名替换
text = re.findall('(<div class="single-text".*?)<div class="sg-tag"', res.text, re.S)[0]text = re.sub(r'<img src="http:/(.*?)" alt="(.*?)">', '<a href="D:/liudahtml\g<1>.jpg" target="_blank"><img src="D:/liudahtml\g<1>.jpg" alt="\g<2>"></a>', text)
找最长的单词
找单词的长度是5或6
普通的列表推导式 map filter
列出首字母大写的单词
[w for w in text if w.istitle()][w for w in text if re.search(r'^[A-Z]',w)]
使用最频繁的单词
from collections import CounterCounter(text).most_common(2)
字符串的分割
text = 'liuda|liuda;;;;;;;liuda\tliuda're.split(r'[|\t;]+', text)['liuda', 'liuda', 'liuda', 'liuda']
字符串的拼接
lists = ['my', 'name', 'is', 'liuda']' '.join(lists)Out[38]: 'my name is liuda
lists = ['i', 'have', 133, 'apples']' '.join(map(str, lists))'i have 133 apples'
字符串的删除
删除字符串中间不符合条件的字符
s = '-- -hello*()python***'re.sub(r'[\-()*\s]+','', s)Out[46]: 'hellopython'
阅读全文
0 0
- 秘籍总结:玩转python里的字符串上篇
- python知识点总结--上篇
- Python基础学习教程(三)之字符串的秘籍
- python里查看string的常量字符串
- Python里的格式化字符串方法format
- 转【上古秘籍】之Eclipse的秘籍
- Python里统计一个字符串中子字符串的个数
- Python中字符串的总结
- 玩转RobotFramework下的Web自动化测试(二)——关于上篇文章的一些思考
- 玩转python里面的小小知识点
- 玩转 Python 3.5 的 await/async
- 玩转字符串2
- 玩转字符串!
- 指针玩转字符串
- 玩转字符串
- 指针玩转字符串
- 玩转字符串
- 玩转常量字符串
- 又是undefined reference to xxx ------库顺序问题
- 笔试查漏补缺17.9.30
- 接口----应用
- 51nod 1711 平均数
- 【ssm框架】增删改查的最佳实践
- 秘籍总结:玩转python里的字符串上篇
- 常用的sql指令总结
- 查看linux系统重启之前的log -- last_kmsg
- Java基础之hashMap相关知识
- Qt 学习之路 2(48):QSortFilterProxyModel
- Android Studio support:appcompat-v7:26+
- 通过sql server 连接mysql
- Dynamic Programming--Algorithms
- Registration has not been run for all of the FEAT directories