Python字符串split学习一(自定义字符串拆分)
来源:互联网 发布:知彼定位软件 编辑:程序博客网 时间:2024/05/18 01:55
开始学习python,第二天,尽管python内建函数split已经能干很多事情,但是遇到这种情况就要自己定义函数了:
string = ' wang__chuang_____bo '
#coding=utf-8#string split#author wangchuanbo#time 2013-09-20s = '&aa ab ac ad&'#去掉字符串开头和结尾的指定字符s1 = s.strip('&')print s1#步骤一:先找第一个单词a = s1.find(" ")word1 = s1[:a]print 'the first word is:',word1#步骤二:找第二个单词while s1[a] == " ": a = a + 1b = s1.find(" ",a)word2 = s1[a:b]print 'the second word is:',word2#自定义的拆分方法#string:要拆分的字符串,delimiter:分隔符,begin_end:字符串开始和结尾标识符def my_split(string,delimiter,begin_end=" "): string = string.strip(begin_end) a = string.find(delimiter) first_word = string[:a] result = [] result.append(first_word) b = a while b <= len(string) and b != -1: while string[a] == delimiter: a = a + 1 b = string.find(delimiter,a) if b != -1: res = string[a:b] else: res = string[a:] a = b result.append(res) return result s1 = '&adfs sdfjklsj dd de&' #如果仅是拆除空格s1.split()可以搞定print my_split(s1," ","&")s2 = ' jkj**jkjk*jkkk****d 'print my_split(s2,"*")s3 = ' wang__chuan_____bo 'print my_split(s3,'_')s4 = ' wang..xiao.....yuan 'print my_split(s4,'.')
- Python字符串split学习一(自定义字符串拆分)
- MySql拆分字符串(split拆分查找)
- SPLIT 拆分字符串
- 字符串拆分split
- 字符串的拆分split
- split拆分字符串
- 拆分字符串Split用法
- split()方法字符串拆分
- sql 拆分字符串 Split
- split方法拆分字符串
- c++ split字符串拆分
- Python split() 函数 拆分字符串 将字符串转化为列
- Python split() 函数 拆分字符串 将字符串转化为列
- Python split() 函数 拆分字符串 将字符串转化为列
- Split()拆分字符串与StringTokenizer拆分字符串
- SQL_字符串拆分(split)方法总结
- 字符串操作方法split(),拆分赋值
- split拆分、组合id字符串
- VC 树形控件(CTreeCtrl)的使用
- c语言 指针和二维数组
- 诗词歌赋
- hdu 4740
- HDU 1573 X问题
- Python字符串split学习一(自定义字符串拆分)
- 并行性与并发性的区别
- C/C++中的内置宏定义 以及 CRT
- H.264专利介绍
- 向循环有序链表内插入一个节点
- MySQL 错误记录
- 非阻塞模式WinSock编程入门
- Guava缓存器源码分析——删除消息
- C#获取客户端计算机硬件及系统信息