How to make “int” parse blank strings?

后端 未结 4 1023
野性不改
野性不改 2020-12-29 21:34

I have a parsing system for fixed-length text records based on a layout table:

parse_table = [\\
    (\'name\', type, length),
    ....
    (\'numeric_field\         


        
4条回答
  •  被撕碎了的回忆
    2020-12-29 22:03

    Use a factory function instead of int or a subclass of int:

    def mk_int(s):
        s = s.strip()
        return int(s) if s else 0
    

提交回复
热议问题