def check_input_data(self, args): input_data = {} if args.input_file: if not os.path.exists(args.input_file): print("File %s is not exist." % args.input_file) raise RuntimeError else: input_data = txt_parser.parse(args.input_file, use_strip=True) elif args.text_1 and args.text_2: if args.text_1.strip() != '' and args.text_2.strip() != '': if six.PY2: input_data = { "text_1": [ args.text_1.strip().decode( sys_stdin_encoding()).decode("utf8") ], "text_2": [ args.text_2.strip().decode( sys_stdin_encoding()).decode("utf8") ] } else: input_data = { "text_1": [args.text_1], "text_2": [args.text_2] } else: print( "ERROR: The input data is inconsistent with expectations.") if input_data == {}: print("ERROR: The input data is inconsistent with expectations.") raise DataFormatError return input_data
def main(): 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:])
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
def to_unicode(self, texts): """ Convert each element's type(str) of texts(list) to unicode in python2.7 Args: texts(list): each element's type is str in python2.7 Returns: texts(list): each element's type is unicode in python2.7 """ if six.PY2: unicode_texts = [] for text in texts: if not isinstance(text, unicode): unicode_texts.append( text.decode(sys_stdin_encoding()).decode("utf8")) else: unicode_texts.append(text) texts = unicode_texts return texts
def parse(self, csv_file): with codecs.open(csv_file, "r", sys_stdin_encoding()) as file: content = file.read() content = content.split('\n') self.title = content[0].split(',') self.content = {} for key in self.title: self.content[key] = [] for text in content[1:]: if (text == ""): continue for index, item in enumerate(text.split(',')): title = self.title[index] self.content[title].append(item) return self.content
if not sub_command in BaseCommand.command_dict: print("ERROR: unknown command '%s'" % sub_command) help.command.execute(argv) exit(1) return False command = BaseCommand.command_dict[sub_command] return command.execute(argv[1:]) command = HubCommand.instance() def main(): 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__": 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:])
def parse(self, yaml_file): with codecs.open(yaml_file, "r", sys_stdin_encoding()) as file: content = file.read() return yaml.load(content, Loader=yaml.BaseLoader)