python如何提取文本指定内容
要提取文本中的指定内容,你可使用以下几种方法:
例如,假定你要提取文本中的所有电话号码,可使用以下代码:
import re
text = "我的电话号码是:1234567890,你的电话号码是:9876543210。"
phone_numbers = re.findall(r'd{10}', text)
print(phone_numbers)
输出结果:
['1234567890', '9876543210']
例如,假定你要提取文本中的所有单词,可使用以下代码:
text = "Hello, World! How are you today?"
words = text.split()
print(words)
输出结果:
['Hello,', 'World!', 'How', 'are', 'you', 'today?']
希望以上方法对你有所帮助!
TOP