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?
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()
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?
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()