尝试使用python在csv文件中查找空白列

时间:2016-05-16 10:19:42

标签: python python-2.7

import csv

f = open('E:\pythontest\ip_data.csv')
csv_f = csv.reader(f)
for row in csv_f:
    row_count = sum(1 for row in csv_f) + 1

print row_count     

现在我正在尝试查找具有空格并计算它们的列。

1 个答案:

答案 0 :(得分:1)

以下内容如何:

import csv

dict_count = {}
count = 0

with open ("E:\pythontest\ip_data.csv", 'r') as f:
 reader = csv.DictReader(f)
 data = list(reader)
 fields = reader.fieldnames

for field in range(len(fields)):
 for k,v in data[field].items():
  if v is "":
   dict_count[k] = str(count + 1)

for k,v in dict_count.items():
 print k,v