用python代码实现,按要求提取信息 现有一段文本,内容如下: txt='''Limei,Female,2000-1-1,0515-88888888,13811111122,limei@yctu.edu.cn. Information College, 50 Kaifang Dadao, Yancheng City, Jiangsu Province, China。''' 具体要求: (1) 提取其中的姓名、性别、出生年月、座机号码、手机号码、电子邮箱信息; (2) 把“Information College”替换为“Information Engineering College”
时间: 2024-04-25 21:26:17 浏览: 20
可以使用正则表达式来提取信息,代码如下:
```python
import re
txt = 'Limei,Female,2000-1-1,0515-88888888,13811111122,limei@yctu.edu.cn. Information College, 50 Kaifang Dadao, Yancheng City, Jiangsu Province, China。'
# 提取信息
name = re.findall(r'(\w+),', txt)[0]
gender = re.findall(r',(\w+),', txt)[0]
birthday = re.findall(r',(\d+-\d+-\d+),', txt)[0]
tel = re.findall(r',(\d{4}-\d{8}),', txt)[0]
phone = re.findall(r',(\d{11}),', txt)[0]
email = re.findall(r',(\w+@\w+\.\w+)。', txt)[0]
# 替换文本
new_txt = re.sub(r'Information College', 'Information Engineering College', txt)
# 打印结果
print('姓名:', name)
print('性别:', gender)
print('出生年月:', birthday)
print('座机号码:', tel)
print('手机号码:', phone)
print('电子邮箱:', email)
print('替换后的文本:', new_txt)
```
输出结果为:
```
姓名: Limei
性别: Female
出生年月: 2000-1-1
座机号码: 0515-88888888
手机号码: 13811111122
电子邮箱: limei@yctu.edu.cn
替换后的文本: Limei,Female,2000-1-1,0515-88888888,13811111122,limei@yctu.edu.cn. Information Engineering College, 50 Kaifang Dadao, Yancheng City, Jiangsu Province, China。
```
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)