New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows(Python 2.x) 命令行下输出日志中文乱码的问题 #80

Closed
wfeixu opened this Issue Apr 4, 2017 · 8 comments

Comments

Projects
None yet
4 participants
@wfeixu

wfeixu commented Apr 4, 2017

2012-04-24 INFO [stop loss: 002570.XSHE 300]
[###########-------------------------] 32%2012-05-02 WARN 璁㈠崟琚嫆鍗? [600476.XSHG] 宸叉定鍋溿€
2012-05-02 WARN 璁㈠崟鍒涘缓澶辫触: 涓嬪崟閲忎负0銆
2012-05-02 WARN 璁㈠崟鍒涘缓澶辫触: 涓嬪崟閲忎负0銆
2012-05-02 WARN 璁㈠崟鍒涘缓澶辫触: 涓嬪崟閲忎负0銆
2012-05-02 WARN 璁㈠崟鍒涘缓澶辫触: 涓嬪崟閲忎负0銆
2012-05-02 WARN 璁㈠崟鍒涘缓澶辫触: 涓嬪崟閲忎负0銆
2012-05-02 WARN 璁㈠崟鍒涘缓澶辫触: 涓嬪崟閲忎负0銆
2012-05-02 WARN 璁㈠崟鍒涘缓澶辫触: 涓嬪崟閲忎负0銆
2012-05-02 WARN 璁㈠崟鍒涘缓澶辫触: 涓嬪崟閲忎负0銆

@wh1100717

This comment has been minimized.

Member

wh1100717 commented Apr 5, 2017

@wfeixu 请问具体的环境是什么样的?

  • python 版本
  • RQAlpha 版本
  • Windows 版本/ Linux 版本
  • 执行的代码 及对应的参数
@wh1100717

This comment has been minimized.

Member

wh1100717 commented Apr 5, 2017

@wfeixu 另外您可以试一下 develop 分支 里面修复了不少关于Python 2 中文乱码的问题

@wfeixu

This comment has been minimized.

wfeixu commented Apr 5, 2017

用的python 2.7,RQAlpha是2.0.6,Win10,配置文件如下:

__config__ = {
    "base": {
        "data_bundle_path":"C:\\Users\\Administrator\\.rqalpha\\",
        "securities": ["stock"],
        "start_date": "2010-01-01",
        "end_date": "2017-03-09",
        "frequency": "1d",
        "stock_starting_cash": 1000000,
        "benchmark": "000905.XSHG",
        "run_type": "b",
    },
    "extra": {
        "log_level": "info",
    },
    "validator": {
        "cash_return_by_stock_delisted": True,
    },
    "mod": {
        "sys_progress": {
            "enabled": True,
            "show": True,
        },
        "sys_simulation": {
            "commission_multiplier": 1,
            "slippage":0.01,
            "matching_type": "current_bar",
        },
        "sys_analyser":{
            #"report":True,
            #"output-file":"G:\\temp\\result.pkl",
            "plot":True,
        }
    },
}
@wh1100717

This comment has been minimized.

Member

wh1100717 commented Apr 5, 2017

@wfeixu 我刚才试了一下 确实在 Windows 下 Python 2.x 会存在乱码的问题,不过这个主要是Windows 下 cmd 是gbk编码。

Python 2.7 中文编码的问题,尤其是 Windows 下中文编码的问题很麻烦,不过这不是RQAlpha的问题,您需要自行处理。

这里给出其中的一种处理方式作为参考:

在初始化回测代码的时候指定编码:

import sys
# sys.setdefaultencoding() does not exist, here!
reload(sys)  # Reload does the trick!
sys.setdefaultencoding('utf-8')

from rqalpha import run

__config__ = {
    "base": {
        "strategy_file": "./examples/buy_and_hold.py",
        "securities": ["stock"],
        "start_date": "2010-01-01",
        "end_date": "2017-03-09",
        "frequency": "1d",
        "stock_starting_cash": 1000000,
        "benchmark": "000905.XSHG",
        "run_type": "b",
    },
    "extra": {
        "log_level": "info",
    },
    "validator": {
        "cash_return_by_stock_delisted": True,
    },
    "mod": {
        "sys_progress": {
            "enabled": True,
            "show": True,
        },
        "sys_simulation": {
            "commission_multiplier": 1,
            "slippage":0.01,
            "matching_type": "current_bar",
        },
        "sys_analyser":{
            #"report":True,
            #"output-file":"G:\\temp\\result.pkl",
            "plot":True,
        }
    },
}

run(__config__)

在 cmd 执行 CHCP 65001 更改控制台编码为utf8

然后执行脚本就会正确显示中文了。

image

@wh1100717 wh1100717 closed this Apr 5, 2017

@wh1100717 wh1100717 changed the title from 日志里的WARN是乱码,这个怎么解决? to RQAlpha 在 Windows(Python 2.x) 命令行下输出日志中文乱码的问题 Apr 5, 2017

@wh1100717 wh1100717 changed the title from RQAlpha 在 Windows(Python 2.x) 命令行下输出日志中文乱码的问题 to Windows(Python 2.x) 命令行下输出日志中文乱码的问题 Apr 5, 2017

@wh1100717 wh1100717 self-assigned this Apr 5, 2017

@wfeixu

This comment has been minimized.

wfeixu commented Apr 5, 2017

可以了,用CHCP 65001,THX

@AI-CN

This comment has been minimized.

AI-CN commented Apr 19, 2017

在win764位系统,anaconda-python2.7_32bit环境中,如果在 cmd 执行 CHCP 65001 更改控制台编码为utf8,在命令行界面执行conda list命令会出现错误,提示:LookupError: unknown encoding: cp65001

@wh1100717

This comment has been minimized.

Member

wh1100717 commented Apr 20, 2017

@AI-CN 试试这个 http://stackoverflow.com/a/35177906

我开发和工作环境不太涉及到WIndows Python 2.7 console 的环境,所以也没怎么测试,可能还是需要自行 google 一下解决方案。

@zhao0305

This comment has been minimized.

zhao0305 commented Nov 21, 2017

in parse_user_config, exception: 'utf8' codec can't decode byte 0xd4 in position 312: invalid continuation byte

日志的乱码没有了,但是报错了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment