开发者

How to make "int" parse blank strings?

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

parse_table = [\
    ('name', type, length),
    ....
    ('numeric_field', int, 10), # int example
    ('textc_field', str, 100), # string example
    ...
]

The idea is that given a table for a message type, I just go through the string, and reconstruct a dictionary out of it, according to entries in the table.

Now, I can handle strings and proper integers, but int() will not parse all-spaces fields (for a good reason, of course).

I wanted to handle it by defining a subclass of int that handles blank strings. This way I could go and change the type of appropriate table entries without introducing additional kludges in the parsing code (like filters), and it would "just work".

But I can't figure out how to override the constructor of a build-in type in a sub-type, as defining constructor in the subclass does not seem to help. I feel I'm 开发者_JAVA技巧missing something fundamental here about how Python built-in types work.

How should I approach this? I'm also open to alternatives that don't add too much complexity.


Use int() function with the argument s.strip() or 0, i.e:

int(s.strip() or 0)

Or if you know that the string will always contain only digit characters or is empty (""), then just:

int(s or 0)

In your specific case you can use lambda expression, e.g:

parse_table = [\
    ....
    ('numeric_field', lambda s: int(s.strip() or 0), 10), # int example
    ...
]


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


lenient_int = lambda string: int(string) if string.strip() else None
                                                          #else 0
                                                          #else ???


note that mylist is a list that contain:

Tuples, and inside tuples, there are I) null / empty values, ii) digits, numbers as strings, as well iii) empty / null lists. for example:

mylist=[('','1',[]),('',[],2)]

@Arlaharen I am repeating here, your solution, somewhat differently, in order to add keywords, because, i lost a lot of time, in order to find it!

The following solution is stripping / converting null strings, empty strings, or otherwise, empty lists, as zero, BUT keeping non empty strings, non empty lists, that include digits / numbers as strings, and then it convert these strings, as numbers / digits.

Simple solution. Note that "0" can be replaced by iterable variables. Note the first solution cannot TREAT empty lists inside tuples.

int(mylist[0][0]) if mylist[0][0].strip() else 0

I found even more simpler way, that IT can treat empty lists in a tuple

int(mylist[0][0] or '0')

convert string to digits / convert string to number / convert string to integer strip empty lists / strip empty string / treat empty string as digit / number convert null string as digit / number / convert null string as integer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜