Python: Reading text file and splitting into different lists
<p>I have a text file which contains two lines of text. Each line contains student names separated by a comma.</p>

<p>I'm trying to write a program which will read each line and convert it to a list. My solution seems to make two lists but I don't know how to differentiate between the two as both lists are called "filelist". For example I may need to append to the second list. How would I differentiate between the two?</p>

<p>Or is it possible for it to create completely separate lists with different names? I want the program to be able to handle many lines in the text file ideally.</p>

<p>My code is:</p>

<pre><code>    filelist=[]

    with open("students.txt") as students:

            for line in students:

                    filelist.append(line.strip().split(","))


    print(filelist)
</code></pre>

<p>You will have to create a multi-dimensional array like this:</p>

<pre><code>text = open("file.txt")

lines = text.split("\n")
entries = []
for line in lines:
    entries.append(line.split(","))
</code></pre>

<p>If your file is</p>

<pre><code>John,Doe
John,Smith
</code></pre>

<p>then entries will be:</p>

<pre><code>[["John", "Doe"], ["John", "Smith"]]
</code></pre>

python list file text