Strip white spaces from CSV file

前端 未结 7 1246
梦谈多话
梦谈多话 2020-12-15 17:02

I need to stripe the white spaces from a CSV file that I read

import csv

aList=[]
with open(self.filename, \'r\') as f:
    reader = csv.reader(f, delimite         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 17:12

    The most memory-efficient method to format the cells after parsing is through generators. Something like:

    with open(self.filename, 'r') as f:
        reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE)
        for row in reader:
            yield (cell.strip() for cell in row)
    

    But it may be worth moving it to a function that you can use to keep munging and to avoid forthcoming iterations. For instance:

    nulls = {'NULL', 'null', 'None', ''}
    
    def clean(reader):
        def clean(row):
            for cell in row:
                cell = cell.strip()
                yield None if cell in nulls else cell
    
        for row in reader:
            yield clean(row)
    

    Or it can be used to factorize a class:

    def factory(reader):
        fields = next(reader)
    
        def clean(row):
            for cell in row:
                cell = cell.strip()
                yield None if cell in nulls else cell
    
        for row in reader:
            yield dict(zip(fields, clean(row)))
    

提交回复
热议问题