修改python列表中的元素

2024-04-28 21:38:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python编程新手,我已经导入了一个文件并将其加载到一个列表中。问题是我想删除列表中每个元素中第二个分隔符之后的所有内容

with open(schemaFile) as f:
    schema = [x.strip('\n') for x in f.readlines()]
    schema = schema[2:]

以下是示例输出:

./ingestor.py -i txt.txt -s kh_offers_schema4.txt4 -o txt.txt
['record_id|string|FALSE|1|', 'offer_id|decimal|FALSE|1|1,1', 'decision_id|decimal|FALSE|1|1,1', 'offer_type_cd|integer|FALSE|1|1,1', 'promo_id|decimal|FALSE|1|1,1', 'pymt_method_type_cd|decimal|FALSE|1|1,1', 'cs_result_id|decimal|FALSE|1|1,1', 'cs_result_usage_type_cd|decimal|FALSE|1|1,1', 'rate_index_type_cd|decimal|FALSE|1|1,1', 'sub_product_id|decimal|FALSE|1|1,1', 'campaign_id|decimal|FALSE|1|1,1', 'market_cell_id|decimal|FALSE|1|1,1', 'assigned_offer_id|string|FALSE|1|', 'accepted_offer_flag|string|FALSE|1|', 'current_offer_flag|string|FALSE|1|', 'offer_good_until_date|string|FALSE|1|', 'rescindable_days|decimal|FALSE|1|1,1', 'rescinded_date|string|FALSE|1|', 'amount|decimal|FALSE|1|1,1', 'max_amount|decimal|FALSE|1|1,1', 'amount_financed|decimal|FALSE|1|1,1', 'down_pymt|decimal|FALSE|1|1,1', 'rate|decimal|FALSE|1|1,1', 'term_mm|decimal|FALSE|1|1,1', 'origination_fee_amount|decimal|FALSE|1|1,1', 'origination_fee_rate|decimal|FALSE|1|1,1', 'finance_charge|decimal|FALSE|1|1,1', 'nbr_of_pymts|decimal|FALSE|1|1,1', 'pymt|decimal|FALSE|1|1,1', 'total_pymts|decimal|FALSE|1|1,1', 'first_pymt_date|string|FALSE|1|', 'contract_date|string|FALSE|1|', 'acct_nbr|string|FALSE|1|', 'acct_nbr_assigned_dttm|string|FALSE|1|', 'acct_expiration_dttm|string|FALSE|1|', 'offer_desc|string|FALSE|1|', 'min_rate|decimal|FALSE|1|1,1', 'max_rate|decimal|FALSE|1|1,1', 'min_amount|decimal|FALSE|1|1,1', 'annual_fee_amount|decimal|FALSE|1|1,1', 'annual_fee_waived_mm|decimal|FALSE|1|1,1', 'late_fee_percent|decimal|FALSE|1|1,1', 'late_fee_min_amount|decimal|FALSE|1|1,1', 'offer_sales_script|string|FALSE|1|', 'offer_order|decimal|FALSE|1|1,1', 'presentable_flag|string|FALSE|1|', 'index_rate|decimal|FALSE|1|1,1', 'insrt_dttm|string|FALSE|1|', 'insrt_usr_id|string|FALSE|1|', 'chng_dttm|string|FALSE|1|', 'chng_usr_id|string|FALSE|1|', 'actv_flag|string|FALSE|1|', 'correlation_id|string|FALSE|1|', 'offer_status_type_cd|string|FALSE|1|', 'presentation_instrument_nbr|string|FALSE|1|']

我只想: ['record|id|string'、'offer|id| decimal'等

有什么办法可以做到这一点吗


Tags: txtidfalsedatestringratetypecd
1条回答
网友
1楼 · 发布于 2024-04-28 21:38:09
SEP = "|"

with open("txt.txt") as inf:
    lines = (line.rstrip() for line in inf)
    items = (line.split(SEP, 2) for line in lines)
    results = [SEP.join(item[:2]) for item in items]

相关问题 更多 >