简体   繁体   English

如何在python中识别csv文件中的空白字段?

[英]How do I identify the blank fields in the csv file in python?

I have a csv file with 5 columns:我有一个包含 5 列的 csv 文件:

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

where the columns represent id, customerId, customerName, customerAddress and customerAge.其中列表示 id、customerId、customerName、customerAddress 和 customerAge。 I want to put 0 at the place where the age is blank and '' where the other string type attributes are blank.我想在年龄为空的地方放0 ,在其他字符串类型属性为空的地方放'' But I can't identify the blank field in python.但是我无法识别python中的空白字段。 I have tried doing some things like:我试过做一些事情,比如:

  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?我究竟做错了什么?

you want to use not你想用not

0 , None , False , '' are all not True 0 , None , False , ''都不是not True

if not row[4]:

you could also do你也可以这样做

bool(row[4])

which will return False for all the above mentioned values这将为上述所有值返回False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM