Part 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Reading and writing

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

L = ["Blue\n", "Green\n", "Red\n", "Black\n", "Pink\n", "Gray\n", " Orange" ]

# 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

dataPt, time, height, error = np.loadtxt("MyData.txt", skiprows=4 , unpack=True)

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

directly read into arrays.

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

reads in only columns 1 and 2; and skips columns 0 and 3 .

- 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”.

Writing Data using savetxt from numpy


one very the simplest method and suitable for writing data files in text format. is the NumPy savetxt routine. The general form of the routine is
savetxt(filename, array, fmt="%0.18e", delimiter=" ", newline="\n", header="", footer="", comments="# ")
Example:
import numpy as np
dataPt, time, height, error = np.loadtxt("MyData.txt", 4 skiprows=5, unpack=True)
np.savetxt('MyDataOut.txt', 7 zip(dataPt, time, height, error), fmt="%12.1f")
Because we want to write more than one arrays to the file, we have to package the four data arrays as one, which we do using the zip function, a
Python function that combines returns a list of tuples. Note that the first two arguments, the filename and data array, are regular arguments and
thus must appear as the first and second arguments in the correct order. The remaining arguments are all keyword arguments, meaning that they
are optional and can appear in any order, provided you use the keyword.
12.1f, which is a float displayed with 1 digit to the right of the decimal point and a minimum width of 12. This is optional and without it, the format will be
0.18e, which displays numbers as 18 digit floats in exponential (scientific) notation.

We also can add the optional header or footer keyword argument,

import numpy as np

dataPt, time, height, error = np.loadtxt("MyData.txt", 4 skiprows=5, unpack=True)

info = 'Data for falling mass experiment'

info += '\nDate: 16-Aug-2013'

info += '\nData taken by Lauren and John'

info += '\n\n data point time (sec) height (mm) '

info += 'uncertainty (mm)'

np.savetxt('MyDataOut.txt', 13 zip(dataPt, time, height, error), header=info, fmt="%12.1f")


Random Numbers in Python
Simulations of many physical processes applications (model physical phenomena) require using a set of numbers with a
random value. Python has a set of functions that are used to generate or manipulate random numbers through the
random module.
The basic idea of a random number generator is that it should be able to produce a sequence of numbers that are
distributed according to some predetermined distribution function.
NumPy provides a number of such random number generators in its library numpy.random or one can import random package.
We used one before which is random.shuffle.

Here some of useful function in random module Python Random Module - GeeksforGeeks

Using random
Function Name Description

seed() Initialize the random number generator

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

getrandbits() Return an integer with a specified number of bits

randrange() Returns a random number within the range


Function Name Description

randint() Returns a random integer within the range

choice() Returns a random item from a list, tuple, or string

choices() Returns multiple random elements from the list with replacement

sample() Returns a particular length list of items chosen from the sequence

random() Generate random floating numbers

uniform() Return random floating number between two numbers both inclusive

expovariate() Return a random floating point number with exponential distribution

gauss() Return a random floating point number with Gaussian distribution

normalvariate() Return a random floating point number with normal distribution

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)

Gaussian distribution using random.gauss

# import the required libraries


import random
import matplotlib.pyplot as plt

# store the random numbers in a


# list
nums = []
mu = 40
sigma = 5

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

Example Uniform and gaussian


import numpy.random as npr
import matplotlib.pyplot as plt
a, b = 10, 20
UD= (b-a) * npr.rand(50) + a # random numbers uniformly distributed from 10 to 20
print(UD)

x0, sigma = 15, 10


ND= sigma*npr.randn(50) + x0 # random numbers normally distributed around 15 with a width of 10.
#plotting a graph
plt.subplot(1,2,1)
plt.hist(UD, bins=10)
plt.subplot(1,2,2)
plt.hist(ND,bins=10)
plt.show()

You might also like