Teemo
Teemo

Reputation: 71

How to split a value to be two records in python without pandas?

I can build the json result with this code.

MY code:

import csv
import json
from os import sep

with open('tran_test.csv') as file:
    dict_input = csv.reader(file, delimiter='|')
    next(dict_input, None)
    output = []
    for line in dict_input:
        promotion_code = line[0]
        promotion_name = line[1]
        coupon_code = line[2]
        coupon_name = line[3]
        coupon = {}
        
        if coupon_code == '' and promotion_code !='':
            pro_code = {}
            pro_code['coupon_code'] = promotion_code
            pro_code['type'] = 'promo_code'
            pro_code['description'] = promotion_name
            pro_code['redeem'] = 'false'
            coupon = [pro_code]
        elif coupon_code != '' and promotion_code == '':
            cou_code = {}
            cou_code['coupon_code'] = coupon_code
            cou_code['type'] = 'coupon_code'
            cou_code['description'] = coupon_name
            cou_code['redeem'] = 'false'
            coupon = [cou_code]
        elif (coupon_code != '' and promotion_code !=''):
            both_promo = {}
            both_coup = {}
            both_promo['coupon_code'] = promotion_code
            both_promo['type'] = 'promo_code'
            both_promo['description'] = promotion_name
            both_promo['redeem'] = 'false'
            both_coup['coupon_code'] = coupon_code
            both_coup['type'] = 'coupon_code'
            both_coup['description'] = coupon_name
            both_coup['redeem'] = 'false'
            coupon = (both_promo , both_coup)
        else:
            None

        output_obj = {}
        output_obj['coupons'] = coupon
        output.append(output_obj)

print(json.dumps(output))

And the result like this:

[{"coupons": [{"coupon_code": "T000003155 ~ E000005182", "redeem": "false", "type": "promo_code", "description": "Net Spend - $800 Enjoy $80 off ~ Net Spend $2000 Enjoy 15%* off"}, {"coupon_code": "0494040", "redeem": "false", "type": "coupon_code", "description": "new join $300"}]}]

The sample csv:

Promotion Code|Promotion Name|Coupon Code|Coupon Name
T000003155 ~ E000005182|Net Spend - $800 Enjoy $80 off ~ Net Spend $2000 Enjoy 15%* off |0494040|new join $300

I want to split the promotion code and promotion name. This depends on '~'. And create two records separately.

Desired result:

[{"coupons": [{"coupon_code": "T000003155", "redeem": "false", "type": "promo_code", "description": "Net Spend - $800 Enjoy $80 off"},{"coupon_code": "E000005182", "redeem": "false", "type": "promo_code", "description": "Net Spend $2000 Enjoy 15%* off"}, {"coupon_code": "0494040", "redeem": "false", "type": "coupon_code", "description": "new join $300"}]}]

How can I do this?

Upvotes: 0

Views: 49

Answers (1)

Martin Wettstein
Martin Wettstein

Reputation: 2904

In order to disentangle the entries in this CSV, you have to find the characters that delimit the values and split the string accordingly. As the format is quite strange with two entries separated by " ~ " in some cases, you'd have to do it manually:

line = "T000003155 ~ E000005182|Net Spend - $800 Enjoy $80 off ~ Net Spend $2000 Enjoy 15%* off |0494040|new join $300"

chunks = line.split("|")
coupon_codes = chunks[0].split(" ~ ")+[chunks[2]]
contents = chunks[1].split(" ~ ")+[chunks[3]]

c = [{"coupons": [{"coupon_code":coupon_codes[0],
                   "redeem":"false",
                   "type":"promo_code",
                   "description":contents[0]},
                  {"coupon_code":coupon_codes[1],
                   "redeem":"false",
                   "type":"promo_code",
                   "description":contents[1]},
                  {"coupon_code":coupon_codes[2],
                   "redeem":"false",
                   "type":"coupon_code",
                   "description":contents[2]}]}]


print(c)

Which returns your desired output:

[{'coupons': [{'coupon_code': 'T000003155', 'redeem': 'false', 'type': 'promo_code', 'description': 'Net Spend - $800 Enjoy $80 off'}, {'coupon_code': 'E000005182', 'redeem': 'false', 'type': 'promo_code', 'description': 'Net Spend $2000 Enjoy 15%* off '}, {'coupon_code': '0494040', 'redeem': 'false', 'type': 'coupon_code', 'description': 'new join $300'}]}]

This only works, however, if the CSV is well-formed and each line has the same delimiters in the same cells.

Upvotes: 1

Related Questions