Python文本数据处理

1、文本基本操作

text1 = 'Python is a widely used high-level programming language for general-purpose programming, created by Guido van Rossum and first released in 1991.'
# 字符个数
print(len(text1))

# 获取单词
text2 = text1.split(' ')
print('单词个数:', len(text2))
# 找出含有长度超过3的单词
print([w for w in text2 if len(w) > 3])
# 找出首字母大写的单词
print([w for w in text2 if w.istitle()])
# 以字母s结尾的单词
print([w for w in text2 if w.endswith('s')])
# 找出不重复的单词
text3 = 'TO be or not to be'
text4 = text3.split(' ')
print('单词个数:', len(text4))
print('不重复的单词个数:', len(set(text4)))
# 忽略大小写统计
set([w.lower() for w in text4])
print(len(set([w.lower() for w in text4])))

2、 文本清洗

text5 = '            A quick brown fox jumped over the lazy dog.  '
text5.split(' ')
print(text5)
text6 = text5.strip()
print(text6)
text6.split(' ')
# 去掉末尾的换行符
text7 = 'This is a line\n'
text7.rstrip()
print(text7)

3、 正则表达式

text8 = '"Ethics are built right into the ideals and objectives of the United Nations" #UNSG @ NY Society for Ethical Culture bit.ly/2guVelr @UN @UN_Women'
print(text8)
text9 = text8.split(' ')
print(text9)
# 查找特定文本
# #开头的文本
print([w for w in text9 if w.startswith('#')])
# @开头的文本
print([w for w in text9 if w.startswith('@')])
# 根据@后的字符的样式查找文本
# 样式符合的规则:包含字母,或者数字,或者下划线
import re
print([w for w in text9 if re.search('@[A-Za-z0-9_]+', w)])
text10 = 'ouagadougou'
print(re.findall('[aeiou]', text10))
print(re.findall('[^aeiou]', text10))
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值