Programming Blog

aa 본문

카테고리 없음

aa

Go-ong 2020. 11. 30. 23:13
def data_process(stock_cd):
    temp_list = []
    stock_dict = {}
    for data in read_data:
        temp_dict = {}
        if data[15:21] == stock_cd:
            temp_dict = {
                'time': data[:8],
                'code': data[15:21],
                'price': data[26:35]
            }

            temp_list.append(temp_dict)
        stock_dict[stock_cd] = temp_list

    # 종목별 접근 loop
    twv_over_list, tht_under_list, tht_over_list, fth_under_list, fth_over_list = [], [], [], [] ,[]
    for data in stock_dict[stock_cd]:
        if data['time'][:2] == '12' and data['time'][3:5] >= '30':
            twv_over_list.append(int(data['price']))

        if data['time'][:2] == '13' and data['time'][3:5] < '30':
            tht_under_list.append(int(data['price']))

        if data['time'][:2] == '13' and data['time'][3:5] >= '30':
            tht_over_list.append(int(data['price']))

        if data['time'][:2] == '14' and data['time'][3:5] < '30':
            fth_under_list.append(int(data['price']))

        if data['time'][:2] == '14' and data['time'][3:5] >= '30':
            fth_over_list.append(int(data['price']))
        
    if len(twv_over_list) == 0:
        twv_over_list.append(0)
    if len(tht_under_list) == 0:
        tht_under_list.append(0)
    if len(tht_over_list) == 0:
        tht_over_list.append(0)
    if len(fth_under_list) == 0:
        fth_under_list.append(0)
    if len(fth_over_list) == 0:
        fth_over_list.append(0)

    print('{}\t {}\t {}\t {}\t {}'.format(stock_cd, '최초가', '최고가', '최저가', '최종가'))
    print('{0}\t {1:,}\t {2:,}\t {3:,}\t {4:,}'.format('12:30', twv_over_list[0], max(twv_over_list), min(twv_over_list), twv_over_list[-1]))
    print('{0}\t {1:,}\t {2:,}\t {3:,}\t {4:,}'.format('13:00', tht_under_list[0], max(tht_under_list), min(tht_under_list), tht_under_list[-1]))
    print('{0}\t {1:,}\t {2:,}\t {3:,}\t {4:,}'.format('13:30', tht_over_list[0], max(tht_over_list), min(tht_over_list), tht_over_list[-1]))
    print('{0}\t {1:,}\t {2:,}\t {3:,}\t {4:,}'.format('14:00', fth_under_list[0], max(fth_under_list), min(fth_under_list), fth_under_list[-1]))
    print('{0}\t {1:,}\t {2:,}\t {3:,}\t {4:,}'.format('14:30', fth_over_list[0], max(fth_over_list), min(fth_over_list), fth_over_list[-1]))

f = open("data/Hoa_data_20_11.doc", 'r', encoding='cp949')

# 데이터 read
read_data = [line.strip('\n') for line in f.readlines()]
stock_cd_list = []
for data in read_data:
    stock_cd_list.append(data[15:21])

stock_cd_list = list(set(stock_cd_list))
stock_cd_list.sort()

for stock_cd in stock_cd_list:
    data_process(stock_cd)
Comments