Python excel表中 一个列下面的行里既有整型又有字符串类型,该如何去进行一个数值筛选
时间: 2023-02-13 16:59:15 浏览: 50
如果你想在 Python 中筛选 Excel 表格中一列下面的行里既有整型又有字符串类型的数值,可以使用以下方法:
首先,使用 Python 的 openpyxl 库读取 Excel 表格。然后,遍历表格中的每一行,判断该行中某一列的值是否为整型或字符串类型。如果是,就将该值存储在一个列表中。最后,可以使用该列表来进行筛选。
以下是一个例子:
```
import openpyxl
# 读取 Excel 表格
wb = openpyxl.load_workbook('my_table.xlsx')
# 选择表格中的某一个工作表
sheet = wb['Sheet1']
# 遍历表格中的每一行
filtered_values = []
for row in sheet.rows:
# 获取行中某一列的值
value = row[1].value
# 判断值是否为整型或字符串类型
if isinstance(value, (int, str)):
filtered_values.append(value)
# 使用 filtered_values 列表进行筛选
```
希望这能帮到你!
相关问题
如何用python判定一个字符串中既有数字又有字母
可以使用Python内置函数isalnum()判断一个字符串中是否同时包含数字和字母。具体实现代码如下:
```
string = "a1b2c3"
if string.isalnum() and not string.isalpha() and not string.isdigit():
print("字符串中既有数字又有字母")
else:
print("字符串中不同时包含数字和字母")
```
运行结果:
```
字符串中既有数字又有字母
```
python一列字符串类型数据获取字符串中指定字符HK$后面的第一个数值
Sure, I can help you with that! Here's a code snippet to extract the first numeric value after the characters "HK$":
```
import re
data = ["HK$400", "HK$600", "HK$800"]
for d in data:
match = re.search(r"HK\$(\d+)", d)
if match:
print(match.group(1))
```
This will output:
```
400
600
800
```
Let me know if you have any other questions!