python - 如何从csv文件python中跳过非数字行
问题描述
我有示例 csv 文件,其字符串值如下:
1234, san@mail, IN, 001
, ram@mail, IN, 003
1235, john@mail, IN, 004
san-ba, luios@mail, IN, 005
undefined, thomas@mail, IN, 006
我需要跳过文件中 row[0] 中具有空且非数字的行。
预期结果:
1234, san@mail, IN, 001
1235, john@mail, IN, 004
解决方案
您可以尝试将值转换为浮点数,如果失败,则跳过它:
for row in data:
first_val = row[0]
try:
float(first_val)
except ValueError:
continue
# here you use the row, knowing the first value is numerical
print("this row has a numerical value in index 0")
推荐阅读
- html - 有没有办法让这个包装?
- sass - 带有 babel-plugin-react-css-modules 的 SASS 嵌套类
- google-chrome-extension - 为什么我会收到 activetab 权限错误?
- c# - 使用属性指定的索引更改 ListViewItem 背景颜色
- java - Kafka Streams 在生产主题时不会将偏移量增加 1
- javascript - 使用pixijs从图像中渲染许多具有六边形形状的精灵
- docker - 未知主机异常
- python - 将数组数组转换为扁平数据框
- c# - 创建while循环时出现问题
- c# - 如何获取非列表的长度?