[Python Study Notes]字符串处理技巧(持续更新)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>>文件: 字符串处理.py
>>作者: liu yang
>>邮箱: liuyang0001@outlook.com
>>博客: www.cnblogs.com/liu66blog ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #!/usr/bin/env python
# -*- coding: utf-8 -*- import sys, os # 1.字符串的连接和合并
# 相加 //两个字符串可以很方便的通过'+'连接起来
str1='123'
str2='456'
str3=str1+str2
print(str3)
# -----------输出----------------------
# 123456
# ------------------------------------ # 合并//用join方法
url=['www','cnblog','com/liu66blog']
print('.'.join(url))
# -----------输出----------------------
# www.cnblog.com/liu66blog
# ------------------------------------ # 2.字符串的切片和相乘
# 相乘//比如写代码的时候要分隔符,用python很容易实现
Separator='*'*30
print(Separator)
# -----------输出----------------------
# ******************************
# ------------------------------------ # 切片操作
url='www.cnblogs.com/liu66blog'
# 取下标0-15个字符
print(url[0:16])
# 取下标16-最后一个
print(url[16:])
# 取倒数第四个到最后
print(url[-4:])
# 复制字符串
print(url[::])
# -----------输出----------------------
# www.cnblogs.com/
# liu66blog
# blog
# www.cnblogs.com/liu66blog
# ------------------------------------ # 3.字符串的分割
# 普通的分割,用split
# split只能做非常简单的分割,而且不支持多个分隔
url='www.cnblogs.com/liu66blog'
url_list=url.split('.')
print(url_list)
# -----------输出----------------------
# ['www', 'cnblogs', 'com/liu66blog']
# ------------------------------------ # 复杂的分割
# r表示不转义,分隔符可以是;或者,或者/,或者空格后面跟0个多个额外的空格,然后按照这个模式去分割
url='www.cnblogs.com/liu66blog'
import re
url_list=re.split(r'[.;/]\s*',url)
print(url_list)
# -----------输出----------------------
# ['www', 'cnblogs', 'com', 'liu66blog']
# ------------------------------------ # 4.字符串的开头和结尾的处理
# 比方我们要查一个名字是以什么开头或者什么结尾
url='www.cnblogs.com/liu66blog'
result=url.endswith('blog')
print(result)
result=url.startswith('ww.')
print(result)
# -----------输出----------------------
# True
# False
# ------------------------------------ # 5.字符串的查找和匹配
# 一般查找
# 我们可以很方便的在长的字符串里面查找子字符串,会返回子字符串所在位置的索引, 若找不到返回-1
url='www.cnblogs.com/liu66blog'
result=url.find('liu66')
print(result)
result=url.find('liuyang')
print(result)
# -----------输出----------------------
# 16
# -1
# ------------------------------------ # 复杂查找
data_str='2018/2/22'
result=re.match(r'\d+/\d+/\d+',data_str)
if result:
print('ok,存在')
# -----------输出----------------------
# ok,存在
# ------------------------------------ # 6.字符串的替换
# 普通的替换//用replace就可以
url='www.cnblogs.com/liu66blog'
url_new=url.replace('www.','')
print(url_new)
# -----------输出----------------------
# cnblogs.com/liu66blog
# ------------------------------------ # 复杂的替换 利用re.sub函数
url='www.cnblogs.com/liu66blog'
url_new=re.sub(r'\d\d','00',url)
print(url_new)
# -----------输出----------------------
# cnblogs.com/liu00blog
# ------------------------------------ # 7.字符串中去掉一些字符
# 去除空格//对文本处理的时候比如从文件中读取一行,然后需要去除每一行的两侧的空格,table或者是换行符
url=' www.cnblogs.com/liu66blog '
url_new=url.strip()
print(url_new) # 复杂的文本清理,可以利用str.translate,
# 先构建一个转换表,table是一个翻译表,表示把'w'转成大写的'W',
# 然后在old_str里面去掉'liu66',然后剩下的字符串再经过table 翻译
# Python3.4已经没有string.maketrans()了,取而代之的是内建函数:
# bytearray.maketrans()、bytes.maketrans()、str.maketrans()
url='www.cnblogs.com/liu66blog' # 创建翻译表
instr='w'
outstr='W'
table=str.maketrans(instr,outstr) url_new=url.translate(table)
print(url_new)
# -----------输出----------------------
# WWW.cnblogs.com/liu66blog
# ------------------------------------ # 8.找最长的单词
txt='Python is a programming language that lets you work more quickly and integrate your systems more effectively. ' \
'You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs. ' \
'Learn more about Python..'
# 使用空格分隔
txt_list=txt.split(' ')
# 使用sorted()函数按照单词长度排序
txt_list_new=sorted(txt_list,key=lambda x:len(x),reverse=True)
# 定义一个空列表,存储最长的
longest_word=[]
# 判断后面的单词长度
for i,word in enumerate(txt_list_new):
if len(txt_list_new[i])<len(txt_list_new[0]):
break
else:
longest_word.append(txt_list_new[i])
print(longest_word)
# -----------输出----------------------
# ['effectively.', 'productivity']
# ------------------------------------ # 9.找出指定长度的单词
len_4_word=filter(lambda x:5>len(x)>=4,txt_list)
# 注意python3 filter返回不再是列表 需要自己转换!!
len_4_word_list=list(len_4_word)
# 转换成去重元祖
len_4_word_tuple=tuple(set(len_4_word_list))
print(len_4_word_list)
print(len_4_word_tuple)
# -----------输出----------------------
# ['that', 'lets', 'work', 'more', 'your', 'more', 'more']
# ('your', 'more', 'lets', 'that', 'work')
# ------------------------------------ # 10.使用最频繁的单词
from collections import Counter
# most_common(x) x代表列举的个数
print(Counter(txt_list).most_common(6))
# -----------输出----------------------
# [('more', 3), ('and', 3), ('Python', 2), ('is', 1), ('a', 1), ('programming', 1)]
# ------------------------------------ # 11.列出所有大写的单词
title_words_list=[]
for i in txt_list:
if i.istitle():
title_words_list.append(i)
# 得到去重字典
title_words_dict=set(title_words_list)
print(title_words_list)
print(title_words_dict)
# -----------输出----------------------
# ['Python', 'You', 'Python', 'Learn', 'Python..']
# {'Python..', 'Learn', 'Python', 'You'}
# ------------------------------------ # 12.未完待续...
[Python Study Notes]字符串处理技巧(持续更新)的更多相关文章
- [Python Study Notes]字符串操作
字符串操作 a.字符串格式化输出 name = "liu" print "i am %s " % name #输出: i am liu PS: 字符 ...
- fastadmin 后台管理框架使用技巧(持续更新中)
fastadmin 后台管理框架使用技巧(持续更新中) FastAdmin是一款基于ThinkPHP5+Bootstrap的极速后台开发框架,具体介绍,请查看文档,文档地址为:https://doc. ...
- PLSQL Developer 11 使用技巧(持续更新)
PLSQL Developer 11 使用技巧 (持续更新) 目录(?)[-] 首先是我的颜色配置 常用快捷键 提升PLSQL编程效率 按空格自动替换 关闭Window窗口 PLSQL 实用技巧 TI ...
- 【Python】【学习笔记】持续更新
调用模块的两种方式: #方式1 from decimal import Decimal Decimal('1.00') #方式2 import decimal decimal.Decimal('1.0 ...
- Python:常见错误集锦(持续更新ing)
初学Python,很容易与各种错误不断的遭遇.通过集锦,可以快速的找到错误的原因和解决方法. 1.IndentationError:expected an indented block 说明此处需要缩 ...
- [Python Study Notes]CS架构远程访问获取信息--Client端v2.0
更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 效果图: '''''''''''''''''''''' ...
- [Python Study Notes]CS架构远程访问获取信息--Client端v1.0
更新内容: 1.添加entry栏默认ip和port口 2.修正退出功能 3.添加退出自动关闭窗口功能 4.优化cpu显示为固定保留两位小数 '''''''''''''''''''''''''''''' ...
- The Python Challenge 谜题全解(持续更新)
Python Challenge(0-2) The Python Challengehttp://www.pythonchallenge.com/ 是个很有意思的网站,可以磨练使用python的技巧, ...
- 个人在 laravel 开发中使用到的一些技巧(持续更新)
1.更高效率地查询:使用批量查询代替 foreach 查询(多次 io 操作转换为一次 io操作) 如果想要查看更详尽的介绍,可以看看这篇文章 什么是 N+1 问题,以及如何解决 Laravel 的 ...
随机推荐
- 怎么在谷歌浏览器中安装.crx扩展名的离线Chrome插件?
李宗申 2014-9-26 23:33:33 20人评论 分类:实用方法 摘要 : 如果用户得到的离线版的Chrome插件文件(扩展名为.crx),该如何将其安装到谷歌浏览器Chrome中去呢? ...
- Struts2 04--- 值栈 ognl(S标签)
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言,通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整 ...
- phpcms v9——工作需要【套模板】
phpcms v9 模板标签说明整理 作者:匿名 来源:ChinaZ源码报导 浏览:44061次 2011-6-17 15:52:09 字号:大 中 小 [摘要]本文介绍phpcms v9中模板标签使 ...
- Aliase_小白学Python_Day0_前言
听到有老师介绍,说你为什么不把你的学习过程保存下来,一是当做总结,二是作为分享.我想,也对.这算是我的第一个博客,本次想写写我为什么选择学习Python. 很多人都问过我一个问题,行业那么多,你为什么 ...
- Assembly oth
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 初次真正意义上的接触Eclipse
以前就玩安卓app 用过很多软件比如e4a,iapp,aide,lua,但是这都不是真正意义上的安卓编程软件.大二的时候用过Androidstudio,那个软件配置起来太难了,就搁置了. 这次利用机智 ...
- php 利用Gd库添加文字水印乱码的问题及解决方案
最近一个项目进行了服务器迁移,部署后发现 ,其中一个为图片添加水印文字的功能出现了乱码问题,确认功能代码不存在问题,同时项目代码都是使用UTF-8编码,不存在编码问题,也检查排除了字体文件出现问题的可 ...
- DELL服务器硬件信息采集SHELL脚本
DELL服务器硬件信息采集SHELL脚本最近需要做资产列表,要采集DELL服务器的硬件信息,包括如下信息:1.操作系统信息(类型,版本,内核,平台,主机名)2.主板信息(厂商,机型,序列号)3.CPU ...
- Nginx几种负载均衡算法及配置实例
本文装载自: https://yq.aliyun.com/articles/114683 Nginx负载均衡(工作在七层"应用层")功能主要是通过upstream模块实现,Ngin ...
- java编码GBK的不可映射字符
编译java文件时,提示编码GBK的不可映射字符. 主要原因:windows 默认编码方式为GBK,用javac编译时,中文按照GBK解析,但是文件内容编码格式不是GBK. 解决:若编译单个文件指定编 ...