如何识别python中csv文件中的空白字段? [英] How do I identify the blank fields in the csv file in python?

查看:649
本文介绍了如何识别python中csv文件中的空白字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含5列的csv文件:

I have a csv file with 5 columns:

1, 2312, "A", , 20
2, 8383, "B", "UK", 
3, 3883, , , 45

其中列表示id,customerId,customerName,customerAddress和customerAge。
我想把 0 放在年龄为空白的地方,''类型属性为空。但我不能识别python中的空白字段。我已经尝试做一些事情像:

where the columns represent id, customerId, customerName, customerAddress and customerAge. I want to put 0 at the place where the age is blank and '' where the other string type attributes are blank. But I can't identify the blank field in python. I have tried doing some things like:


  1. len(row [4])== 0

  2. row [4] ==''

  3. row [4] == None

  4. repr(row [4])==''

  1. len(row[4]) == 0
  2. row[4] == ''
  3. row[4] == None
  4. repr(row[4]) == ''

,但无效。我究竟做错了什么?

but it didn't work. What am I doing wrong?

推荐答案

您要使用不是

0 None False '都不是真的

if not row[4]:

/ p>

you could also do

bool(row[4])

会为所有上述值返回 False

这篇关于如何识别python中csv文件中的空白字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆