From 65bfdd2241c67e92e1decf9073b35d9d6c14091f Mon Sep 17 00:00:00 2001 From: msincenselee Date: Sun, 19 Jan 2020 17:15:13 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=A2=9E=E5=BC=BA]=20=E5=A2=9E=E5=8A=A0csv?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=BF=BD=E5=8A=A0=E6=95=B0=E6=8D=AE=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=8A=A8=E6=80=81=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/utility.py | 71 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/vnpy/trader/utility.py b/vnpy/trader/utility.py index 33dcd73c..404e1bc6 100644 --- a/vnpy/trader/utility.py +++ b/vnpy/trader/utility.py @@ -5,6 +5,8 @@ General utility functions. import json import logging import sys +import os +import csv import re from pathlib import Path from typing import Callable, Dict @@ -170,7 +172,6 @@ def extract_vt_symbol(vt_symbol: str): symbol, exchange_str = vt_symbol.split(".") return symbol, Exchange(exchange_str) - def generate_vt_symbol(symbol: str, exchange: Exchange): """ return vt_symbol @@ -304,6 +305,74 @@ def print_dict(d: dict): """返回dict的字符串类型""" return '\n'.join([f'{key}:{d[key]}' for key in sorted(d.keys())]) + +def append_data(self, file_name: str, dict_data: dict, field_names: list = []): + """ + 添加数据到csv文件中 + :param file_name: csv的文件全路径 + :param dict_data: OrderedDict + :return: + """ + dict_fieldnames = sorted(list(dict_data.keys())) if len(field_names) == 0 else field_names + + try: + if not os.path.exists(file_name): + print(u'create csv file:{}'.format(file_name)) + with open(file_name, 'a', encoding='utf8', newline='\n') as csvWriteFile: + writer = csv.DictWriter(f=csvWriteFile, fieldnames=dict_fieldnames, dialect='excel') + print(u'write csv header:{}'.format(dict_fieldnames)) + writer.writeheader() + writer.writerow(dict_data) + else: + with open(file_name, 'a', encoding='utf8', newline='\n') as csvWriteFile: + writer = csv.DictWriter(f=csvWriteFile, fieldnames=dict_fieldnames, dialect='excel', + extrasaction='ignore') + writer.writerow(dict_data) + except Exception as ex: + print(u'append_data exception:{}'.format(str(ex)), file=sys.stderr) + + +def import_module_by_str(import_module_name): + """ + 动态导入模块/函数 + :param import_module_name: + :return: + """ + import traceback + from importlib import import_module, reload + + # 参数检查 + if len(import_module_name) == 0: + print('import_module_by_str parameter error,return None') + return None + + print('trying to import {}'.format(import_module_name)) + try: + import_name = str(import_module_name).replace(':', '.') + modules = import_name.split('.') + if len(modules) == 1: + mod = import_module(modules[0]) + return mod + else: + loaded_modules = '.'.join(modules[0:-1]) + print('import {}'.format(loaded_modules)) + mod = import_module(loaded_modules) + + comp = modules[-1] + if not hasattr(mod, comp): + loaded_modules = '.'.join([loaded_modules,comp]) + print('realod {}'.format(loaded_modules)) + mod = reload(loaded_modules) + else: + print('from {} import {}'.format(loaded_modules,comp)) + mod = getattr(mod, comp) + return mod + + except Exception as ex: + print('import {} fail,{},{}'.format(import_module_name,str(ex),traceback.format_exc())) + + return None + class BarGenerator: """ For: