from string import split, join def chomp(line): """ get rid of a trailling newline """ try: if line[-1] == '\n': line = line[:-1] except IndexError: pass return line class reader: """ read tsv from a file """ def __init__(self, file): self.file = file def read(self): line = self.file.readline() if not line: return None line = chomp(line) values = split(line, "\t") # note - no escaping of \n \t yet return values class writer: """ write tsv to a file """ def __init__(self, file): self.file = file def write(self, values): if values == None: self.file.close() return # note - no escaping of \n \t yet line = join(values, "\t") line = line + "\n" self.file.write(line)