Part 6
Part 6
Part 6
data to files
- To read and write data from or to a file, use the open to create a file object and use the object’s read or write methods
to read or write data.
- A file is placed in a directory in the file system where the code is placed or one can use the absolute filename.
- An absolute filename contains a filename with its complete path and drive letter. For example,
c:\pybook\CP\Scores.txt is the absolute filename for the file Scores.txt on the Windows operating system.
c:\pybook\CP is referred to as the directory path to the file.
- To write data to a file or read the data back from a file, first we need to create a file object that is associated with a
physical file. This is called opening a file. The syntax for opening a file is:
fileVariable = open(filename, mode)
The open function returns a file object for filename. The mode parameter is a string that specifies how the file will be
used (for reading or writing).
Mode Description
"r" Opens a file for reading.
"w" Opens a new file for writing. If the file already exists, its old contents are destroyed.
"a" Opens a file for appending data from the end of the file.
- When you open a file named fx for reading, if the file does not exist, an error occurs. Therefore, to check if the file fx
exists, os.path.isfile(fx) function can be used .
- But opening the same file for writing, if the file does not exist, a new file is created. if the file exists, the existing file
is overwritten with the new file.
- you can use the read method to read a specified number of characters or all characters from the file and return them
as a string, the readline() method to read the next line, and the readlines() method to read all the lines into a list of
strings.
Example 1 :
# Python code to
# demonstrate readlines()
# writing to file
file1 = open('myfile.txt', 'w')
file1.writelines(L)
file1.close()
# Using readlines()
file1 = open('myfile.txt', 'r')
Lines = file1.readlines()
count = 0
# Strips the newline character
for line in Lines:
count += 1
print("Line{}: {}".format(count, line.strip()))
Example 2: Read specific lines
# open the sample file used
file = open(''myfile.txt')
# read the content of the file opened
content = file.readlines()
# read 10th line from the file
print("tenth line")
print(content[9])
# print first 3 lines of file
print("first three lines")
print(content[0:3])
Reading Data using loadtxt from numpy
Assum we have the below data file which named MyData.txt
Data for falling mass experiment
Date: 16-Aug-2013
Data taken by Lauren and John
Datapoint time (sec) height (mm) uncertainty (mm)
0 0.0 180 3.5
1 0.5 182 4.5
2 1.0 178 4.0
3 1.5 165 5.5
4 2.0 160 2.5
5 2.5 148 3.0
6 3.0 136 2.5
7 3.5 120 3.0
- to read these data into a Python program, First, associate the data in each column with an appropriately named array. the simplest by far
is to use the NumPy loadtxt function. Then we can read the data into four different arrays with the following statemen
The loadtxt function takes three arguments: the first is a string that is the name of the file to be read, the second tells loadtxt to skip the
first 4 lines at the top of file, sometimes called the header, and the third tells loadtxt to output the data (unpack the data) so that it can be
- The above is used to read in all the columns of data, But one can specify which columns to read in using the usecols key word. For
example, the call I time, height = loadtxt('MyData.txt', skiprows=5 , usecols = (1,2), unpack=True)
- You can added the argument delimiter=’ , ’ that tells loadtxt that the columns are separated by commas instead of white space (spaces or
tabs)
Note: loadtxt is a NumPy function. To use it in a Python module, you must first include an “import numpy as np” statement before
calling “np.loadtxt”.
import numpy as np
Here some of useful function in random module Python Random Module - GeeksforGeeks
Using random
Function Name Description
getstate() Returns an object with the current internal state of the random number generator
setstate() Used to restore the state of the random number generator back to the specified state
choices() Returns multiple random elements from the list with replacement
sample() Returns a particular length list of items chosen from the sequence
uniform() Return random floating number between two numbers both inclusive
Example ( in IDEL)
import random as ran
ran.randint(4,20) # return integer random between (4,20)
lst1=[ran.randint(0,10) for I in range (0,10)] # randrange(a,b-1) similar to randint(a,b)
print(1lst)
ran.random( ) # float random number between 0 and1
ran.sample(range(0,100),10) 10 random number between 0, 100, No repeating number.
np.random.sample(range(0,100),10)
for i in range(100):
temp = random.gauss(mu, sigma)
nums.append(temp)
#plotting a graph
plt.subplot(1,2,1)
plt.plot(nums)
plt.subplot(1,2,2)
#plot His
plt.hist(nums, bins=10)
plt.show()
Using numpy.random
import numpy.random as npr
npr.rand() #float random number between 0 and1 as uniform distribution
npr.rand(5) # 5 float random numbers between 0 and1
npr. randn() # float random numbers that follows a distribution of mean 0 and standard deviation 1.
npr.randint(10, 20, 5) # The function randint(low, high, num) produces a uniform random distribution