This notesheet look at how to import text files in python, and read and convert the content into more useful formats. For example, into a list of integers or a matrix of floats.
Data can be input directly or indirectly to a code. Direct inputs are provided interactively, while the programme is running (e.g. using a keyboard and the input() function). Indirect inputs are when data is read from a predefined location, such as a file.
Writing to Files
Just like libraries, files need to be uploaded before they can be accessed:
f = open('FileName.txt', 'w')
This opens the file ‘FileName.txt’ to write to it, and assigns it to variable f. To write something to the file, for example a number of strings, use the write command:
# To add strings a and b to the file on separate lines:
f.write(str(a) + '\n')
f.write(str(b) + '\n')
To write from a list:
# To add a list to a file, with each element on a new line:
for item in a:
f.write(str(item) + '\n')
To create a file with numbers from 1 to 100:
f = open('Numbers100.txt', 'w')
R = range(1, 101)
for i in R:
f.write(str(i) + '\n')
f.close()
Reading from Files
Instead of using a ‘w’, an ‘r’ represents reading:
f = open('FileName.txt', 'r')
By default, the program will read a file exactly as it is:
f = open('FileName.txt, 'r')
a = f.read()
f.close()
print(a)
If, however, you want to compile all the lines into a list, use the .readlines command:
f = open('FileName.txt', 'r')
a = f.readlines()
f.close
print(a)
This produces a list of strings with operators ‘\n’ attached. The new line trail can be removed using a strip function:
f = open('FileName.txt', 'r')
a = f.readlines()
f.close()
b = []
for items in a:
b = b + [items.rstrip()]
print(b)
Creating a List of Integers from a .txt File
f = open('IntegerList.txt', 'r')
a = f.readlines()
f.close()
b = []
for items in a:
b = b + [int(items.rstrip())]
Creating a List of Floats from a .txt File
f = open('DecimalList.txt', 'r')
a = f.readlines()
f.close()
b = []
for items in a:
b = b + [float(items.rstrip())]
Creating a float matrix from a .txt File
If you have a text file of decimals – say 12 of them – you can turn these into a matrix of floats using numpy. This code is for a file with 12 decimals, and it is turned into a 4×3 matrix: four rows of three columns.
import numpy as np
f = open('12decimals.txt', 'r')
a = f.readlines()
f.close()
M = []
for items in a:
M = M + [float(items.rstrip())]
M = np.array(M)
M = M.reshape(4,3)
print(M)
If you want to turn a file into a square matrix:
def SquareMatrix(file):
import numpy as np
f = open(file, 'r')
a = f.readlines()
f.close()
M = []
for items in a:
M = M + [float(items.rstrip())]
n = len(M)**0.5
if int(len(M)**0.5)==n:
M = np.array(M)
M = M.reshape(int(n),int(n))
return M
else:
return 'Cannot produce square matrix'
#not the input to the function must be the full file name in quote marks
print(SquareMatrix('12decimals.txt'))
#this one will, obviously, npot produce a square matrix
Closing Files
When finished with a file, it needs to be closed (this is like saving it):
f.close()