# Path: kquant_data/config.py
# __CONFIG_H5_FUT_SECTOR_DIR__ = os.path.join(__CONFIG_H5_FUT_DIR__, 'sectorconstituent')
# 
# Path: kquant_data/wind/wset.py
# def write_constituent(path, df):
#     df.to_csv(path, encoding='utf-8-sig', date_format='%Y-%m-%d', index=False)
# 
# def read_constituent(path):
#     """
#     读取板块文件
#     :param path:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, encoding='utf-8-sig', parse_dates=True)
#     except Exception as e:
#         return None
#     try:
#         df['date'] = pd.to_datetime(df['date'])
#     except KeyError:
#         pass
#     return df


import os
import sys
import pandas as pd
from kquant_data.config import __CONFIG_H5_FUT_SECTOR_DIR__
from kquant_data.wind.wset import write_constituent, read_constituent
# 解决Python 3.6的pandas不支持中文路径的问题
print(sys.getfilesystemencoding())  # 查看修改前的
try:
    sys._enablelegacywindowsfsencoding()  # 修改
    print(sys.getfilesystemencoding())  # 查看修改后的
except:
    pass

if __name__ == '__main__':
    path = os.path.join(__CONFIG_H5_FUT_SECTOR_DIR__, "上期所全部品种")

    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            if filename <= '2017-12-26.csv':
                continue
            # sc2018年3月26号上市
            if filename >= '2018-03-26.csv':
                continue
            new_path = os.path.join(dirpath, filename)

            df_csv = read_constituent(new_path)
            sym_ex = df_csv['wind_code'].str.split('.')
            sym_ex = list(sym_ex)
            sym_ex_df = pd.DataFrame(sym_ex)
            sym_ex_df.columns = ['InstrumentID', 'exchange']
            df = pd.concat([df_csv, sym_ex_df], axis=1)
            df = df[df['exchange'] != 'INE']
            df = df[['wind_code']]

            if len(df) < len(df_csv):

# Path: kquant_data/config.py
# __CONFIG_H5_FUT_SECTOR_DIR__ = os.path.join(__CONFIG_H5_FUT_DIR__, 'sectorconstituent')
# 
# Path: kquant_data/wind/wset.py
# def write_constituent(path, df):
#     df.to_csv(path, encoding='utf-8-sig', date_format='%Y-%m-%d', index=False)
# 
# def read_constituent(path):
#     """
#     读取板块文件
#     :param path:
#     :return:
#     """
#     try:
#         df = pd.read_csv(path, encoding='utf-8-sig', parse_dates=True)
#     except Exception as e:
#         return None
#     try:
#         df['date'] = pd.to_datetime(df['date'])
#     except KeyError:
#         pass
#     return df


# Path: demo_future/C03_update_sector.py
import os
import sys
import pandas as pd
from kquant_data.config import __CONFIG_H5_FUT_SECTOR_DIR__
from kquant_data.wind.wset import write_constituent, read_constituent

# 解决Python 3.6的pandas不支持中文路径的问题
print(sys.getfilesystemencoding())  # 查看修改前的
try:
    sys._enablelegacywindowsfsencoding()  # 修改
    print(sys.getfilesystemencoding())  # 查看修改后的
except:
    pass

if __name__ == '__main__':
    path = os.path.join(__CONFIG_H5_FUT_SECTOR_DIR__, "上期所全部品种")

    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            if filename <= '2017-12-26.csv':
                continue
            # sc2018年3月26号上市
            if filename >= '2018-03-26.csv':
                continue
            new_path = os.path.join(dirpath, filename)

            df_csv = read_constituent(new_path)
            sym_ex = df_csv['wind_code'].str.split('.')
            sym_ex = list(sym_ex)
            sym_ex_df = pd.DataFrame(sym_ex)
            sym_ex_df.columns = ['InstrumentID', 'exchange']
            df = pd.concat([df_csv, sym_ex_df], axis=1)
            df = df[df['exchange'] != 'INE']
            df = df[['wind_code']]

            if len(df) < len(df_csv):

                write_constituent(new_path, df)