How to create a dictionary from a line of text?

前端 未结 4 2126
一生所求
一生所求 2020-11-28 14:13

I have a generated file with thousands of lines like the following:

CODE,XXX,DATE,20101201,TIME,070400,CONDITION_CODES,LTXT,PRICE,999.0000,QUANTITY,100,TSN,151

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 14:36

    import itertools
    
    def grouper(n, iterable, fillvalue=None):
        "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
        args = [iter(iterable)] * n
        return itertools.izip_longest(fillvalue=fillvalue, *args)
    
    record = dict(grouper(2, line.strip().split(","))
    

    source

提交回复
热议问题