Strip white spaces from CSV file

前端 未结 7 1262
梦谈多话
梦谈多话 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

    Read a CSV (or Excel file) using Pandas and trim it using this custom function.

    #Definition for strippping whitespace
    def trim(dataset):
        trim = lambda x: x.strip() if type(x) is str else x
        return dataset.applymap(trim)
    

    You can now apply trim(CSV/Excel) to your code like so (as part of a loop, etc.)

    dataset = trim(pd.read_csv(dataset))
    dataset = trim(pd.read_excel(dataset))
    

提交回复
热议问题