2023/2/18
一、文本的基本操作
text1='Obviously,learning python is not easy at all"
len(text1)
###分别提取text中的字符串
text2=text1.split('')
text3=set([w.lower() for w in text2])#将text中的所有单词小写
二、提取特定的单词
##提取特定长度的单词
[w for w in text1 if len(w)>3]
##提取含有特定字母的单词
[w for w in text1 if w.startswith('s')]
[w for w in text1 if w.endswith('s')]
[w for w in text1 if w.istitle()]#提取首字母大写的单词
text.find('t')
##其他操作
text.strip()#删去文本开头空格
text.replace('s','t')
三、字符处理
s='ouagadougou'
t='ou'
#字符简单识别
s.startswith('ou')
s.endswith('ou')
s.isupper()
s.islower()
s.isalpha()
s.isalnum()
s.istitle()
##字符简单处理
s.lower()
s.upper()
s1=s.split('t')#根据字符t划分s
s.splitlines()
t.join(s1)#s1的每个空格均填充t
s.strip()#去掉开头空格
s.rstrip()
s.find(t)#从前找t
s.rfind(t)
s.replace('ou','g')#将g用ou代替
四、大批量文本操作
f=open('text.txt',r) ##open('filename.txt',mode)
f.readline()
f.seek(1)##f.seek(num)将阅读光标放在num处
text12=f.read()
text13=text12.split()
text14=text12.splitlines()
f.write()
f.close()
五、日期相关文本操作
###提取文件符合格式要求(正则表达式)
text1=text0.split('')
[w for w in text1 if w.startswith('#')]
import re
[w for w in text1 if re.search('@[A-Za-Z0-9_]+',w)]#识别带有A-Z,a-z,0-9或_
#######re.search('callouts[identifier]times',w)
############meta-character匹配特征 .单个字符 ^字符开头 $字符解尾 []一套特征 [^a]不是a的字符
############times *(zero or more) +(at least once) ?(zero or one) {n}(n times)
re.findall(r'[eaiou]',text3)#找所有元音
re.findall(r'[^aeiou]',text3)#所有非元音
##日期
dateStr='23-10-2002\23/10/2002\23/10/02\10/23/2002'
re.findall(r'\d{2}[-/]\d{2}[-/]\d{4}',dateStr)
re.findall(r'\d{2}[-/]\d{2}[-/]\d{2,4}',dateStr)
re.findall(r'\d{2}(Jan|Oct)\d{4}',dateStr)##putout only Oct
re.findall(r'\d{2}(?:Jan|Oct)\d{4}',dateStr)##putout the whole string
re.findall(r'\d{2}(?:Jan|Oct)[a-z]*\d{4}',dateStr)##任何包含Oct的
新手小白,还望不吝赐教