Random File Reader with Delimiter

  • Share this:

Code introduction


This function is used to read a file and return a list of lines, where each line is split into a list by a specified delimiter. It is commonly used to handle CSV files.


Technology Stack : Python, File I/O

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def random_file_reader(file_path, delimiter=','):
    """
    This function reads a file and returns a list of lines where each line is split by a delimiter.
    
    Args:
        file_path (str): The path to the file to be read.
        delimiter (str): The delimiter used to split each line.
    
    Returns:
        list: A list of lists, where each inner list represents a line split by the delimiter.
    """
    lines = []
    with open(file_path, 'r') as file:
        for line in file:
            lines.append(line.strip().split(delimiter))
    return lines                
              
Tags: