How to use lists from a text file in python?
<p>i have been trying to read/write values(lists) in a .txt file and using them later, but i can't find a function or something to help me use these values as lists and not strings, since using the <code>readline</code> function doesn't help.</p>

<p>Also, im don't want to use multiple text files to make up 1 list</p>

<p>example:</p>

<pre><code>v=[]
f = open("test.txt","r+",-1)
f.seek(0)
v.append(f.readline())
print(v)
</code></pre>

<p>in test.txt</p>

<pre><code>cat, dog, dinosaur, elephant
cheese, hotdog, pizza, sushi
101, 23, 58, 23
</code></pre>

<p>im expecting to the list <code>v = [cat, dog, dinosaur, elephant]</code> in separate indexes, but by doing this code (which is totally wrong) i get this instead
<code>v = ['cat,dog,dinosaur,elephant']</code> which is what i don't want</p>

<p>Sounds like you want to read it as comma separated values.</p>

<p>Try the following</p>

<pre><code>import csv
with open('test.txt', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row)
</code></pre>

<p>I believe that will put you on the right track. For more information about how the csv parser works, have a look at the docs</p>

<p><a href="https://docs.python.org/3/library/csv.html" rel="nofollow">https://docs.python.org/3/library/csv.html</a></p>

python list file python-3.x