1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| class Read_table(): ''' 用于处理文本文件进行dict化产生的字典 输入:文本文件 ''' def __init__(self, file, title=None): '''读取文件为字典''' with open(file, 'r') as infile: intitle = infile.readline().strip().split('\t') if title == None else title dic_list = [{} for i in range(len(intitle))] count = 0 for line in infile.readlines(): line = line.strip().split('\t') for i in range(len(line)): dic_list[i][count] = line[i] count += 1 self.dic = dict(zip(intitle, dic_list)) def getdict(self): '''返回原始字典''' return self.dic def getlen(self): '''获取字典元素的长度''' return len(self.dic[list(self.dic.keys())[0]]) def getcol(self, str, match): '''对某一列进行模糊匹配,并返回满足条件的行''' import re return list(filter(lambda i: re.findall(r'%s'%match, self.getvalue(str, i)), list(self.getiter()))) def getiter(self): '''创建可循环的迭代器''' dic_len = self.getlen() return iter(range(dic_len)) def getvalue(self, key, line): '''获取字典的值''' value = str(self.dic[key][line]) if key in self.dic else '-' return value def getline(self, line): '''获取整行信息''' line_list = [str(self.dic[key][line]) for key in self.dic.keys()] return line_list
class Read_table(): ''' 用于处理文本文件进行dict化产生的字典 输入:文本文件 ''' def __init__(self, file, title=None): '''读取文件为字典''' with open(file, 'r') as infile: intitle = infile.readline().strip().split('\t') if title == None else title lines = list(zip(*[line.strip().split('\t') for line in infile.readlines()])) self.dic = dict(zip(intitle, [dict(zip(range(len(lines[0])),i)) for i in lines])) def getdict(self): '''返回原始字典''' return self.dic def getlen(self): '''获取字典元素的长度''' return len(self.dic[list(self.dic.keys())[0]]) def getcol(self, str, match): '''对某一列进行模糊匹配,并返回满足条件的行''' import re return list(filter(lambda i: re.findall(r'%s'%match, self.getvalue(str, i)), list(self.getiter()))) def getiter(self): '''创建可循环的迭代器''' dic_len = self.getlen() return iter(range(dic_len)) def getvalue(self, key, line): '''获取字典的值''' value = str(self.dic[key][line]) if key in self.dic else '-' return value def getline(self, line): '''获取整行信息''' line_list = [str(self.dic[key][line]) for key in self.dic.keys()] return line_list
class Read_table(): ''' 用于处理文本文件进行dict化产生的字典 输入:文本文件 ''' def __init__(self, file, title=None): '''读取文件为字典''' with open(file, 'r') as infile: intitle = infile.readline().strip().split('\t') if title == None else title lines = list(zip(*map(lambda line: line.strip().split('\t'),infile.readlines()))) self.dic = dict(zip(intitle, map(lambda line: dict(zip(range(len(lines[0])), line)), lines))) def getdict(self): '''返回原始字典''' return self.dic def getlen(self): '''获取字典元素的长度''' return len(self.dic[list(self.dic.keys())[0]]) def getcol(self, str, match): '''对某一列进行模糊匹配,并返回满足条件的行''' import re return list(filter(lambda i: re.findall(r'%s'%match, self.getvalue(str, i)), list(self.getiter()))) def getiter(self): '''创建可循环的迭代器''' dic_len = self.getlen() return iter(range(dic_len)) def getvalue(self, key, line): '''获取字典的值''' value = str(self.dic[key][line]) if key in self.dic else '-' return value def getline(self, line): '''获取整行信息''' line_list = [str(self.dic[key][line]) for key in self.dic.keys()] return line_list
class Read_table(): ''' 用于处理文本文件进行dict化产生的字典 输入:文本文件 ''' def __init__(self, file, title=None, skip=None): '''读取文件为字典''' import os filetype = os.path.splitext(file)[-1] if filetype == '.gz': import gzip gzInFile = gzip.open(file, 'rb') intitle = gzInFile.readline().decode().strip('\n').split('\t') if title == None else title if skip != None: if gzInFile.readline().startswith(skip): next lines = list(zip(*map(lambda line: line.decode().strip('\n').split('\t'),gzInFile.readlines()))) self.dic = dict(zip(intitle, map(lambda line: dict(zip(range(len(lines[0])), line)), lines))) gzInFile.close() else: with open(file, 'r') as infile: intitle = infile.readline().strip('\n').split('\t') if title == None else title if skip != None: if infile.readline().startswith(skip): next lines = list(zip(*map(lambda line: line.strip('\n').split('\t'),infile.readlines()))) self.dic = dict(zip(intitle, map(lambda line: dict(zip(range(len(lines[0])), line)), lines))) self.title = intitle def getdict(self): '''返回原始字典''' return self.dic def getTitleList(self): '''返回表头''' return self.title def getlen(self): '''获取字典元素的长度, 即行数''' return len(self.dic[list(self.dic.keys())[0]]) if len(self.dic.keys()) != 0 else 0 def getcol(self, str, match): '''对某一列进行模糊匹配,并返回满足条件的行''' import re return list(filter(lambda i: re.findall(r'%s'%match, self.getvalue(str, i)), list(self.getiter()))) def getiter(self): '''创建可循环的迭代器''' dic_len = self.getlen() return iter(range(dic_len)) def getvalue(self, key, line): '''获取字典的值''' value = str(self.dic[key][line]) if key in self.dic else '-' return value def getValueList(self, keyList, line): '''获取提供list的值''' valueList = [str(self.dic[key][line]) if key in self.dic else '-' for key in keyList] return valueList def getline(self, line): '''获取整行信息''' line_list = [str(self.dic[key][line]) for key in self.dic.keys()] return line_list
|