Пример #1
0
def get_codelist(codepool):
    """
    将各种‘随意’写法的A股股票代码列表,转换为6位数字list标准规格,
    可以是“,”斜杠,“,”,可以是“、”,或者其他类似全角或者半角符号分隔。
    """
    if (isinstance(codepool, str)):
        codelist = [code.strip() for code in codepool.splitlines()]
    elif (isinstance(codepool, list)):
        codelist = codepool
    else:
        print(u'Unsolved stock_cn code/symbol string:{}'.format(codepool))

    ret_codelist = []
    for code in codelist:
        if (len(code) > 6):
            try_split_codelist = code.split('/')
            if (len(try_split_codelist) > 1):
                ret_codelist.extend(try_split_codelist)
            elif (len(code.split(' ')) > 1):
                try_split_codelist = code.split(' ')
                ret_codelist.extend(try_split_codelist)
            elif (len(code.split('、')) > 1):
                try_split_codelist = code.split('、')
                ret_codelist.extend(try_split_codelist)
            elif (len(code.split(',')) > 1):
                try_split_codelist = code.split(',')
                ret_codelist.extend(try_split_codelist)
            elif (len(code.split(',')) > 1):
                try_split_codelist = code.split(',')
                ret_codelist.extend(try_split_codelist)
            elif (code.startswith('XSHE')) or \
                (code.endswith('XSHE')):
                ret_codelist.append('{}.XSHE'.format(QA_util_code_tostr(code)))
                pass
            elif (code.startswith('XSHG')) or \
                (code.endswith('XSHG')):
                #Ztry_split_codelist = code.split('.')
                ret_codelist.append('{}.XSHG'.format(QA_util_code_tostr(code)))
            else:
                if (QA_util_code_tostr(code)):
                    pass
                print(u'Unsolved stock_cn code/symbol string:{}'.format(code))
        else:
            ret_codelist.append(code)

    # 去除空字符串
    ret_codelist = list(filter(None, ret_codelist))
    #print(ret_codelist)

    # 清除尾巴
    ret_codelist = [code.strip(',') for code in ret_codelist]
    ret_codelist = [code.strip('\'') for code in ret_codelist]
    ret_codelist = [code.strip('’') for code in ret_codelist]
    ret_codelist = [code.strip('‘') for code in ret_codelist]

    # 去除重复代码
    ret_codelist = list(set(ret_codelist))

    return ret_codelist
Пример #2
0
def get_display_name_list(codepool):
    """
    将各种‘随意’写法的A股股票名称列表,转换为6位数字list标准规格,
    可以是“,”斜杠,“,”,可以是“、”,或者其他类似全角或者半角符号分隔。
    """
    if (isinstance(codepool, str)):
        codelist = [code.strip() for code in codepool.splitlines()]
    elif (isinstance(codepool, list)):
        codelist = codepool
    else:
        print(u'Unsolved stock_cn code/symbol string:{}'.format(codepool))

    ret_displaynamelist = []
    for display_name in codelist:
        if (len(display_name) > 6):
            try_split_codelist = display_name.split('/')
            if (len(try_split_codelist) > 1):
                ret_displaynamelist.extend(try_split_codelist)
            elif (len(display_name.split(' ')) > 1):
                try_split_codelist = display_name.split(' ')
                ret_displaynamelist.extend(try_split_codelist)
            elif (len(display_name.split('、')) > 1):
                try_split_codelist = display_name.split('、')
                ret_displaynamelist.extend(try_split_codelist)
            elif (len(display_name.split(',')) > 1):
                try_split_codelist = display_name.split(',')
                ret_displaynamelist.extend(try_split_codelist)
            elif (len(display_name.split(',')) > 1):
                try_split_codelist = display_name.split(',')
                ret_displaynamelist.extend(try_split_codelist)
            elif (display_name.startswith('XSHE')) or \
                (display_name.endswith('XSHE')):
                ret_displaynamelist.append('{}.XSHE'.format(
                    QA_util_code_tostr(display_name)))
                pass
            elif (display_name.startswith('XSHG')) or \
                (display_name.endswith('XSHG')):
                #Ztry_split_codelist = code.split('.')
                ret_displaynamelist.append('{}.XSHG'.format(
                    QA_util_code_tostr(display_name)))
            else:
                if (QA_util_code_tostr(display_name)):
                    pass
                print(u'Unsolved stock_cn code/symbol string:{}'.format(
                    display_name))
        else:
            ret_displaynamelist.append(display_name)

    # 去除空字符串
    ret_displaynamelist = list(filter(None, ret_displaynamelist))

    # 清除尾巴
    ret_displaynamelist = [code.strip(',') for code in ret_displaynamelist]
    ret_displaynamelist = [code.strip('\'') for code in ret_displaynamelist]

    # 去除重复代码
    ret_displaynamelist = list(set(ret_displaynamelist))

    # 用中文名反查股票代码
    stock_list = QA.QA_fetch_stock_list()
    dict_stock_list = {
        stock['name'].replace(' ', ''): code
        for code, stock in stock_list.T.iteritems()
    }
    ret_symbol_list = [
        dict_stock_list[display_name] for display_name in ret_displaynamelist
    ]

    return ret_displaynamelist, ret_symbol_list