Skip to content Skip to sidebar Skip to footer

Create List Of Lists From File Python

I'm new to python, I'm trying to create a list of lists from a text file. The task seems easy to do but I don't know why it's not working with my code. I have the following lines

Solution 1:

You can iterate like this:

f = [i.strip('\n').split(',') for i in open('file.txt')]

Solution 2:

Your code works fine, if your code creating issues in your system then if you want you can do this in one line with this :

withopen("file.txt") as f:
    print([i.strip().split(',') for i in f])

Post a Comment for "Create List Of Lists From File Python"