【python文本挖掘】Note1

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的

新手小白,还望不吝赐教

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值