Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

please make a python snippet to load a bdas.txt file and save a list of strings, each…

  1. User

    please make a python snippet to load a bdas.txt file and save a list of strings, each string is a unique line in the file.

  2. Assistant

    Certainly! Here's a Python code snippet that loads a "bdas.txt" file and saves a list of strings where each string corresponds to a unique line in the file:

    def load_txt_file(filename):
        lines = []
        with open(filename, 'r') as file:
            for line in file:
                line = line.strip()  # remove leading/trailing whitespaces
                if line not in lines:
                    lines.append(line)
        return lines
    
    # Provide the filename/path of the bdas.txt file
    filename = "bdas.txt"
    
    # Load the file and save unique lines in a list
    unique_lines = load_txt_file(filename)
    
    # Print the list of unique lines
    for line in unique_lines:
        print(line)
    

    Ensure to replace "bdas.txt" with the actual filename or full path to the "bdas.txt" file you want to load. The code reads the file, strips any leading/trailing whitespaces, and appends each unique line to the lines list. Finally, it prints the unique lines to demonstrate how they can be accessed.