'Opening two different format files with a single statement

I am Reading 2 files .txt and .tsv, i had 2 different methods to read these type of files but i want to do it in a single way. for .tsv file we have to pass an argument delimiter and for .txt files it does not need any argument. How can i pass delimiter argu? so that i would not need separate functions.

class DataReader:

def __init__(self, **type="tsv"**):
    self.weather_records = []

def read(self, path: str, file: str):
    with open(path + file) as file:
        for line in csv.DictReader(file, delimiter="\t"):
            self.__load(line)


Solution 1:[1]

You can try this. I have added comments in the code below.

class DataReader:
    def __init__(self, type="tsv"): #type doesn't matter for file reading anymore
        self.weather_records = []

    def read(self, path, file):
        with open(path + file) as file:
            if file.endswith('.tsv'):
                #do something with delimiter
                #You could also set the type here, if you want to use it later
            else:
                #do without delimiter.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Manjunath K Mayya