写一段python代码,提取文本中的小写字母
时间: 2023-02-23 20:09:51 浏览: 69
以下是一段 Python 代码,它可以提取文本中的小写字母:
```
text = "Hello, World! 123"
result = "".join(filter(str.islower, text))
print(result)
```
输出:
```
elloorld
```
在这段代码中,首先定义了一个变量 `text`,存储了要提取的文本。然后,使用 `filter` 函数和 `str.islower` 方法进行筛选,只保留文本中的小写字母。最后,使用 `"".join` 将筛选出的小写字母合并成一个字符串,并通过 `print` 语句输出。
相关问题
写一段Python代码提取图像轮廓
下面是一段使用 OpenCV 库提取图像轮廓的 Python 代码:
```
import cv2
import numpy as np
# 读取图像
img = cv2.imread("image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用 Canny 边缘检测算法检测边缘
edges = cv2.Canny(gray, 50, 150)
# 查找图像中的轮廓
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 在原图上绘制轮廓
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
# 显示结果图像
cv2.imshow("Contours", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在代码中,首先我们读取了图像,并将其转换为灰度图。然后,使用 Canny 边缘检测算法检测图像中的边缘。最后,使用 `cv2.findContours()` 函数查找图像中的轮廓,并在原图上绘制出轮廓,最后显示结果图像。
写一段python代码提取页面数据
以下是提取页面数据的 Python 代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 提取页面标题
title = soup.title.string
print('页面标题:', title)
# 提取页面所有链接
links = []
for link in soup.find_all('a'):
links.append(link.get('href'))
print('页面链接:', links)
# 提取页面所有图片链接
img_links = []
for img in soup.find_all('img'):
img_links.append(img.get('src'))
print('页面图片链接:', img_links)
```
以上代码使用 requests 库获取页面内容,使用 BeautifulSoup 库解析页面,然后提取页面标题、链接和图片链接。