def test_split(self): split_list = string_utils.split('data test world', ' ') self.assertEqual(len(split_list), 3) self.assertEqual(split_list[0], 'data') self.assertEqual(split_list[1], 'test') self.assertEqual(split_list[2], 'world') split_list = string_utils.split('data ; test ; world', ';') self.assertEqual(len(split_list), 3) self.assertEqual(split_list[0], 'data') self.assertEqual(split_list[1], 'test') self.assertEqual(split_list[2], 'world')
def awk(target, pattern, separator, action): ''' awk : pattern scanning and text processing language @param target: string list or text file name @param pattern: regex pattern @param separator: line separator @param action: column index list or function: str[]=action(str[]) @summary: list= ['1:huiyugeng:male', '2:zhuzhu:male'] print awk.awk(list, '', ':', [1]) output: ['huiyugeng', 'zhuzhu'] ''' text = grep.grep(target, pattern) if not text: return None if str_utils.is_blank(separator): separator = ' ' result = [] for line in text: if line != None and separator in line: split_text = str_utils.split(line, separator) if not action: result.append(split_text) elif type(action) == types.ListType: temp_line = [] for column in action: if str_utils.is_numeric(column): temp_line.append(split_text[column]) result.append(temp_line) elif type(action) == types.FunctionType: temp_line = action(split_text) if temp_line != None and type(temp_line) == types.ListType: result.append(temp_line) return result
def match_line(line): if string_utils.is_not_empty(line): return len(string_utils.split(line, ';')) == 5 return False