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))

猜你喜欢

转载自blog.csdn.net/happy5205205/article/details/80913360
今日推荐