From ef3a19310c3c16c5d956ed19096f908b4502c3e2 Mon Sep 17 00:00:00 2001 From: Steffy-zxf <48793257+Steffy-zxf@users.noreply.github.com> Date: Fri, 31 May 2019 10:14:29 +0800 Subject: [PATCH] fix-encoding-format (#44) --- paddlehub/commands/hub.py | 8 +++++++- paddlehub/io/parser.py | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/paddlehub/commands/hub.py b/paddlehub/commands/hub.py index c5f901cc..877b3faf 100644 --- a/paddlehub/commands/hub.py +++ b/paddlehub/commands/hub.py @@ -58,7 +58,13 @@ command = HubCommand.instance() def main(): - command.execute(sys.argv[1:]) + argv = [] + for item in sys.argv: + if six.PY2: + argv.append(item.decode(sys.stdin.encoding).decode("utf8")) + else: + argv.append(item) + command.execute(argv[1:]) if __name__ == "__main__": diff --git a/paddlehub/io/parser.py b/paddlehub/io/parser.py index 4a60105a..a3c7b4aa 100644 --- a/paddlehub/io/parser.py +++ b/paddlehub/io/parser.py @@ -17,6 +17,8 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function +import codecs +import sys import yaml @@ -28,7 +30,7 @@ class CSVFileParser(object): pass def parse(self, csv_file): - with open(csv_file, "r") as file: + with codecs.open(txt_file, "r", sys.stdin.encoding) as file: content = file.read() content = content.split('\n') self.title = content[0].split(',') @@ -55,7 +57,7 @@ class YAMLFileParser(object): pass def parse(self, yaml_file): - with open(yaml_file, "r") as file: + with codecs.open(txt_file, "r", sys.stdin.encoding) as file: content = file.read() return yaml.load(content, Loader=yaml.BaseLoader) @@ -67,10 +69,14 @@ class TextFileParser(object): def _check(self): pass - def parse(self, yaml_file): - with open(yaml_file, "r") as file: - contents = file.read().split("\n") - return [content for content in contents if content] + def parse(self, txt_file): + with codecs.open(txt_file, "r", sys.stdin.encoding) as file: + contents = [] + for line in file: + line = line.strip() + if line: + contents.append(line) + return contents csv_parser = CSVFileParser() -- GitLab