本文共 1020 字,大约阅读时间需要 3 分钟。
1.将第6章内容写成一个对象进行解析
import parse from decimal import Decimalimport deloreanclass PriceLog(): def __init__(self,timesptamp,product_id,price): self.timesptamp = timesptamp self.product_id = product_id self.price = price def __repr__(self): return '<PriceLog ({},{},{})>'.format(self.timesptamp,self.product_id,self.price) @classmethod def parse(cls,text_log): ''' Parse from a text log with the format [<Timestamp>] - SALE - PRODUCT: <product_id> -PRICE: $<PRICE> to a PriceLog object ''' divide_id = text_log.split(' - ') tmp_string,_,product_string,price_string = divide_id timesptamp = delorean.parse(tmp_string.strip('[]')) product_id = int(product_string.split(':')[-1]) price =Decimal(price_string.split('$')[-1]) return cls(timesptamp=timesptamp,product_id=product_id,price=price)log = '[2021-02-05T11:07:12.267897] - SALE - PRODUCT: 1345 - PRICE: $09.99'a = PriceLog.parse(log)print(a) |
2.输入log日志值后打印输出结果
<PriceLog (Delorean(datetime=datetime.datetime(2021, 5, 2, 11, 7, 12, 267897), timezone='UTC'),1345,9.99)> |
转载地址:http://kcuti.baihongyu.com/