Create a list or array from a text file in Python
Here’s our task: We have a text file containing numerical data. We would like to read the file contents into a Numpy array and a Python list. Use the np.loadtxt() function to write your text into an array and the file object read() function to populate a Python list
#1 Data Preparation
Assume that you have a text file that contains the following comma delimited values:
Here’s is the code in order to create the file:
numbers = "0,1,2,3.5,4.5,32.1,4,2.2,4,62,1" file_path =r'C:\WorkDir\numbers.txt' with open (file_path, 'w') as my_file: my_file.write(numbers)
Aside: Note that you’ll need to convert ensure that the numbers variable is a string and not a tuple or list here as other wise you will receive a type error:
TypeError: write() argument must be str, not list # or tuple, or int if passed to the file object write function
#2 Read text file into Numpy array
We can use the numpy loadtxt() method in order to read a text or comma separated csv file into an ndarray object. Remember to import the numpy library into your namespace before invoking np.loadtxt().
import numpy as np numbers_array = np.loadtxt(r'C:\WorkDir\numbers.txt', delimiter=',')
We can then easily look into the array contents:
This will return the following ndarray object:
[ 0. 1. 2. 3.5 4.5 32.1 4. 2.2 4. 62. 1. ]
Note: unless a delimiter is specified you will get a value error:
ValueError: could not convert string to float
#3 Read csv or txt into list
Similarly, we can also add the contents of a file into a Python:
with open (file_path, 'r') as my_file: numbers_lst = my_file.read().split(',') print(numbers_lst)
This will result in a Python list of strings. We can use a simple list comprehension to convert it to integers.
['0', '1', '2', '3.5', '4.5', '32.1', '4', '2.2', '4', '62', '1']
Explanation: We first open the text or csv file for read only, we then use the read() function to add the content of the file into a string object. Last, because the values in our file are delimited by commas, we use the string split method to populate our list.
Note: use the readlines() function to read each row in your file into a list element, and the readline() function in order to read each row in your file line by line.