json_sort.py 4.74 KB
import json
from operator import itemgetter

'''
with open('coffee_temp.json', 'r', encoding='utf-8') as read_file:
    temp_dict = json.load(read_file)


'''
'''
a = ['맥심', 'CP20', '동서', '롯데', '㉢맥심', '노브랜드', '동서식품', '코카콜라', 'FE)', '롯데칠성', '식품', 'OnlyPrice', '㉢', 'HACCP인증']

coffee = []
coffee = temp_dict['product']

for i in range(len(커피)):
    for j in range(len(a)):
        if coffee[i]['name'].find(a[j]) != -1:
            coffee[i]['name'] = coffee[i]['name'].replace(a[j], '')

coffee_dict = {}
coffee_dict['product'] = coffee


with open('coffee_temp.json', 'w', encoding='utf-8') as make_file:
    json.dump(coffee_dict, make_file, ensure_ascii=False, indent='\t')
'''
'''
coffee = []
coffee = temp_dict['product']

emart_list = []
homeplus_list = []
lottemart_list = []

new_list = []

for i in range(len(커피)):
    if coffee[i]['name'] == '' or coffee[i]['price'] == '' or coffee[i]['unit'] == '' or coffee[i]['img'] == '':
        continue
    if coffee[i]['mart'] == 'emart':
        if coffee[i]['name'] not in emart_list:
            emart_list.append(coffee[i]['name'])
            new_list.append(coffee[i])

    elif coffee[i]['mart'] == 'homeplus':
        if coffee[i]['name'] not in homeplus_list:
            homeplus_list.append(coffee[i]['name'])
            new_list.append(coffee[i])

    elif coffee[i]['mart'] == 'lottemart':
        if coffee[i]['name'] not in lottemart_list:
            lottemart_list.append(coffee[i]['name'])
            new_list.append(coffee[i])

new_list = sorted(new_list, key=itemgetter('name'))
coffee_dict = {}
coffee_dict['product'] = new_list

with open('coffee.json', 'w', encoding='utf-8') as make_file:
    json.dump(coffee_dict, make_file, ensure_ascii=False, indent='\t')

'''
'''
coffee_sort = sorted(coffee, key=itemgetter('name'))

print(coffee_sort)

'''
'''
with open('json/snack.json', 'r', encoding='utf-8') as read_file:
    temp_dict = json.load(read_file)


snack_keyword = ['(오리지널)', '농심', '롯데', '오리온', '노브랜드', '해태', '삼양', 'UP)', '(지퍼/대)', '삼양', 'CJ',
                 'MISURA', '삼진', '★리뉴얼런칭특가★', '(통)', '서광농협', '제주', '(안복자한과)',  '@', 'V)', '2입',
                 '31', '동서식품', '동서', '12P', '15곡+2견과', '청우식품청우)', 'P)해태NEW', '5P', '(신)', 'H)', '4번들',
                 '청우식품', '15+', '동원', 'CJ', 'NEW', 'P)', '빙그레', 'GD)', 'simplus', 'V)', '한진식품', '6팩', '오뚜기', '90g*2',
                 '270g', '30입', '머거본 ', '한진', '12입', '3번들', '삼진', 'ACTⅡ', 'OnlyPrice', ' ', 'Hav′eat', '1P']

snack = []
snack = temp_dict['product']

for i in range(len(snack)):
    for j in range(len(snack_keyword)):
        if snack[i]['name'].find(snack_keyword[j]) != -1:
            snack[i]['name'] = snack[i]['name'].replace(snack_keyword[j], '')

snack_dict = {}
snack_dict['product'] = snack

with open('snack_temp.json', 'w', encoding='utf-8') as make_file:
    json.dump(snack_dict, make_file, ensure_ascii=False, indent='\t')

'''


with open('snack_temp.json', 'r', encoding='utf-8') as read_file:
    temp_dict = json.load(read_file)
emart_list = []
homeplus_list = []
lottemart_list = []
snack = []
snack = temp_dict['product']
new_list = []

for i in range(len(snack)):
    if snack[i]['name'].find('G') != -1:
        temp = snack[i]['name'].find('G')
        for j in range(temp-1, 0, -1):
            if ord(snack[i]['name'][j]) < 48 or ord(snack[i]['name'][j]) > 57:
                snack[i]['name'] = snack[i]['name'][:j+1]
                break
    if snack[i]['name'].find('g') != -1:
        temp = snack[i]['name'].find('g')
        for j in range(temp-1, 0, -1):
            if ord(snack[i]['name'][j]) < 48 or ord(snack[i]['name'][j]) > 57:
                snack[i]['name'] = snack[i]['name'][:j+1]
                break
    if snack[i]['name'] == '' or snack[i]['price'] == '' or snack[i]['unit'] == '' or snack[i]['img'] == '':
        continue
    if snack[i]['mart'] == 'emart':
        if snack[i]['name'] not in emart_list:
            emart_list.append(snack[i]['name'])
            new_list.append(snack[i])

    elif snack[i]['mart'] == 'homeplus':
        if snack[i]['name'] not in homeplus_list:
            homeplus_list.append(snack[i]['name'])
            new_list.append(snack[i])

    elif snack[i]['mart'] == 'lottemart':
        if snack[i]['name'] not in lottemart_list:
            lottemart_list.append(snack[i]['name'])
            new_list.append(snack[i])

new_list = sorted(new_list, key=itemgetter('name'))
snack_dict = {}
snack_dict['product'] = new_list
with open('snack.json', 'w', encoding='utf-8') as make_file:
    json.dump(snack_dict, make_file, ensure_ascii=False, indent='\t')