page contents

Python 20个进阶必看的文本处理函数!

今天给大家分享 -20个进阶必看的文本处理Python函数。

attachments-2025-02-kh5YB4Ce67b53222254e7.png

今天给大家分享 -20个进阶必看的文本处理Python函数。

1、str.strip()

移除字符串两端的空白字符或指定字符。示例:

text = "  hello, world!  "cleaned_text = text.strip()print(cleaned_text)  

2、 str.lstrip()和str.rstrip()

分别用于去除左边和右边的指定字符。

3、str.split()

根据指定的分隔符将字符串分割成列表。示例:

data = "apple, banana, cherry"fruits = data.split(", ")print(fruits)  

4、 str.replace()

替换字符串中的某部分文本。示例:

text = "i love programming in python"new_text = text.replace("python""javascript")print(new_text)  

5、 str.join()

将列表中的元素连接成一个字符串。示例:

words = ("hello""world")sentence = " ".join(words)print(sentence)  

6、str.find()

查找子字符串的位置,只返回第一次出现的位置。示例:

text = "python is fun!"position = text.find("fun")print(position)  

7、 re.findall()

使用正则表达式从字符串中找出所有匹配的子字符串。示例:

import retext = "my phone numbers are +1-555-1234 and +1-555-5678."numbers = re.findall(r'\+\d{1,3}-\d{3}-\d{4}', text)print(numbers)  

8、re.sub()

使用正则表达式替换字符串中的子字符串。示例:

import retext = "my phone numbers are +1-555-1234 and +1-555-5678."new_text = re.sub(r'\+\d{1,3}-\d{3}-\d{4}''xxx-xxx-xxxx', text)print(new_text)  

9、str.lower()和str.upper()

将字符串转换为全小写或全大写。示例:

text = "Hello, World!"lower_text = text.lower()upper_text = text.upper()print(lower_text)print(upper_text)

10、str.startswith()和str.endswith()

检查字符串是否以指定的前缀或后缀开头或结尾。示例:

text = "hello, world!"starts_with_hello = text.startswith("hello")ends_with_world = text.endswith("world")print(starts_with_hello)print(ends_with_world)

11、string.capwords()

把一个字符串中的所有单词首字母大写。示例:

import strings = "the quick brown fox jumped over the lazy dog."print(string.capwords(s))

12、 len()

获取字符串的长度。示例:

text = "helloworld"length = len(text)print(length)

13、textwrap.fill()

格式化文本段落,将文本按照指定宽度进行换行。

14、textwrap.indent()

为字符串中的所有行增加一致的前缀文本。

15、re.compile()

编译正则表达式模式,用于后续的匹配操作,可提高效率。示例:

import repattern = re.compile(r'\d+')text = "hello123world"matches = pattern.findall(text)print(matches)

16、 map()

对可迭代对象中的每个元素应用指定的函数,并返回一个新的可迭代对象。示例:

def square(x):    return x  2numbers = [12345]squared_numbers = map(square, numbers)print(list(squared_numbers))

17、filter()

根据指定的函数过滤可迭代对象中的元素,返回符合条件的元素组成的新可迭代对象。示例:

def is_even(x):    return x % 2 == 0numbers = [123456]even_numbers = filter(is_even, numbers)print(list(even_numbers))

18、 enumerate()

用于将一个可迭代对象组合为一个索引序列,同时列出元素和其索引。示例:

words = ["apple""banana""cherry"]for i, word in enumerate(words):    print(f"{i}{word}")

19、 zip()

用于将多个可迭代对象中的元素一一对应地组合成元组,返回一个包含这些元组的可迭代对象。示例:

names = ["Alice""Bob""Charlie"]ages = [253035]for name, age in zip(names, ages):    print(f"{name} is {age} years old.")

20、 set()

将字符串转换为集合,可用于去重以及集合运算。示例:

text = "hello world hello python"word_set = set(text.split())print(word_set)

结语

代码写完了,赶紧试试吧!

更多相关技术内容咨询欢迎前往并持续关注好学星城论坛了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
小柒
小柒

1738 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1738 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章