Importing array list in text file into Python

Question:

I have an array list in a txt file like:

[1, 2, 3, 4, 5]

I would like to get this data into an array in my python code. Afterall, when I print it in Python I should see below output:

[1, 2, 3, 4, 5]

(Exactly same with content in txt.)

Is that possible? If yes, how?

Asked By: Yusuf Ziya Güleç

||

Answers:

To read the data from your txt file and store it as a list in Python, you can use the following code:


with open('path/to/your/file.txt', 'r') as file: data = file.read()
myList = data.split()
Answered By: Dylan Brosseau
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.