Making a text into a list without including certain variables

Issue

I am given a problem where I have to open a text file and then make it a list. This is the text file:

Ronald,               80,       182
Ethan L,   73,       175

Firstly I will open it:

def function():
    text =('users.txt','r')
    text.read().# this is where I want to filter away all the spaces and ','
    return text

This problem is very advanced, is there any way I can use the python functions or will I have to import something?

Solution

def function():
    result = []

    with open('text.txt', 'r') as file:
        raw_txt = file.read()  # this is where I want to filter away all the spaces and ','

    lines = raw_txt.splitlines()

    for line in lines:
        result.append(list(map(lambda e: e.strip(), line.split(","))))
    

    return result

Answered By – prnvbn

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published