Accessing csv header white space and case insensitive

前端 未结 2 1622
予麋鹿
予麋鹿 2021-01-23 13:44

I\'m overriding the csv.Dictreader.fieldnames property like the following to read all headers from csv files without white space and in lower case.

         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 14:14

    You'll have to do it in two steps:

    1. Create your dict specialisation with a __getitem__ method that applies the .strip().lower() to the its key parameter.
    2. Override __next__ on your MyDictReader specialised class to return one of your special dictionaries initialised with the dictionary returned by the csv.DictReader superclass's __next__ method.

提交回复
热议问题