使用python csv reader忽略“blank”(未填充)行

时间:2018-01-16 22:50:00

标签: python csv reader

大家。

我无法找到一种pythonic方法来忽略CSV中的“空白”行。我使用引号是因为我说的是看起来像'','','','',''的行 这是一个CSV(空行可以是随机的):

id,name,age
1,alex,22
3,tiff,42
,,
,,
4,john,24

以下是代码:

def getDataFromCsv(path):
    dataSet = []
    with open(unicode(path), 'r') as stream:
        reader = csv.reader(stream, delimiter=',')
        reader.next() # ignoring header
        for rowdata in reader:
            # how to check here?
            dataSet.append(rowdata)
    return dataSet

以下是我一直在阅读的类似问题,但特别是与此不同: python csv reader ignore blank row

5 个答案:

答案 0 :(得分:8)

您可以使用any检查行中的任何列是否包含数据:

Go to you package.json and change all @angular dependencies to 4.0.0 or any version you want.  
Then do the same for @angular-cli with the version number you want it to be. just like the code below

- Save that file    
- Delete you node-modules folder  
- run `npm install` or `yarn` if you are using yarn. 




 {
    "name":"crud-operation",
     "version":"0.0.0",
     "license":"MIT",
     "scripts":{
     "ng":"ng",
          "start":"ng serve",
          "build":"ng build --prod",
          "test":"ng test",
          "lint":"ng lint",
          "e2e":"ng e2e"
       },
       "private":true,
       "dependencies":{
          "@angular/animations":"4.0.0",
          "@angular/common":"4.0.0",
          "@angular/compiler":"4.0.0",
          "@angular/core":"4.0.0",
          "@angular/forms":"4.0.0",
          "@angular/http":"4.0.0",
          "@angular/platform-browser":"4.0.0",
          "@angular/platform-browser-dynamic":"4.0.0",
          "@angular/router":"4.0.0",
          "core-js":"^2.4.1",
          "rxjs":"^5.5.6",
          "zone.js":"^0.8.19"
       },
       "devDependencies":{
          "@angular/cli":"1.6.3",
          "@angular/compiler-cli":"4.0.0",
          "@angular/language-service":"4.0.0",
          "@types/jasmine":"~2.8.3",
          "@types/jasminewd2":"~2.0.2",
          "@types/node":"~6.0.60",
          "codelyzer":"^4.0.1",
          "jasmine-core":"~2.8.0",
          "jasmine-spec-reporter":"~4.2.1",
          "karma":"~2.0.0",
          "karma-chrome-launcher":"~2.2.0",
          "karma-cli":"~1.0.1",
          "karma-coverage-istanbul-reporter":"^1.2.1",
          "karma-jasmine":"~1.1.0",
          "karma-jasmine-html-reporter":"^0.2.2",
          "protractor":"~5.1.2",
          "ts-node":"~3.2.0",
          "tslint":"~5.9.1",
          "typescript":"~2.5.3"
   }
}

答案 1 :(得分:0)

危险区域.. 也许是在重温旧线程..

为什么不使用过滤器?然后我认为大型 csv 文件没有内存问题。

类似于:

std::declval

答案 2 :(得分:-1)

怎么样:

for rowdata in reader:
    # how to check here?
    if any(x.strip() for x in rowdata):
        dataSet.append(rowdata)

或者我错过了你问题的一部分?

答案 3 :(得分:-1)

You can use the built-in function any:

for rowdata in reader:
    # how to check here?
    if not any(row):
        continue
    dataSet.append(rowdata)

答案 4 :(得分:-1)

with open(fn, 'r') as csvfile:
    reader = csv.reader(csvfile)
    data = [row for row in reader if any(col for col in row)]
  • 打开CSV文件
  • 实例化csv.reader()对象
  • 使用列表推导来:
    • 遍历CSV行
    • 迭代行中的列
    • 检查行中是否有任何列,如果有,将其添加到列表中