Example python3: popShow0 – simple file-reader

eJournal: uffmm.org,
ISSN 2567-6458, 6.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de


This is a possible 5th step in the overall topic ‘Co-Learning python3′. After downloading WinPython and activating the integrated editor ‘spyder’ (see here),  one can edit another simple program dealing with population dynamics in a most simple way (see the source code below under the title ‘EXAMPLE: popShow0.py’). This program is a continuation of the program pop0e.py, which has been described here.


In this post I comment only on the changes between the actual program and the version before.


In this program the following two libraries are used:

import numpy as np # Lib for math
from tkinter.filedialog import askopenfilename

numpy is known from previous programs while tkinter is here used to enable a file dialog to find a certain file while browing the different folders.

The program opens such a windows for browsing:

print(‘A window is asking you for a filename\n’)
infilename = askopenfilename()

The variable ‘infilename’ is a variable for strings, which in this case saves the content of the file.  This content looks like this:

# br=0.03,dr=0.019
# X-Column, Y-Column
1.000000000000000000e+00   7.466964000000000000e+06
2.000000000000000000e+00   7.549100604000000283e+06
3.000000000000000000e+00   7.632140710644000210e+06
4.000000000000000000e+00   7.716094258461084217e+06
5.000000000000000000e+00   7.800971295304155909e+06
6.000000000000000000e+00   7.886781979552501813e+06
7.000000000000000000e+00   7.973536581327578984e+06
8.000000000000000000e+00   8.061245483722181991e+06
9.000000000000000000e+00   8.149919184043126181e+06
1.000000000000000000e+01    8.239568295067600906e+06
1.100000000000000000e+01    8.330203546313344501e+06
1.200000000000000000e+01    8.421835785322790965e+06
1.300000000000000000e+01    8.514475978961341083e+06
1.400000000000000000e+01    8.608135214729916304e+06
1.500000000000000000e+01    8.702824702091945335e+06
1.600000000000000000e+01    8.798555773814957589e+06

This context will then be formatted by the following lines:

data = np.loadtxt(infilename)
x = data[:, 0]
y = data[:, 1]

The leading header will automatically be discarded and the main content will be stored in two columns. These formatted two-columns data will then be printed with:

for i in range(len(x)):
print(‘Year %d = Citizens. %9.0f \n’ % (x[i],y[i]))

For each of the values to print x[i] and y[i] there are formatting options telling that the x[i] represents a ‘year’ understood as an integer, and  that y[i] represents the population number Citizens understood as a floting point number with zero signs behind the floating point.

The output looks then like this (Date are from the UN for 2016; the simulation computes this into a possible future):

Year 1 = Citizens. 7466964

Year 2 = Citizens. 7549101

Year 3 = Citizens. 7632141

Year 4 = Citizens. 7716094

Year 5 = Citizens. 7800971

Year 6 = Citizens. 7886782

Year 7 = Citizens. 7973537

Year 8 = Citizens. 8061245

Year 9 = Citizens. 8149919

Year 10 = Citizens. 8239568

Year 11 = Citizens. 8330204

Year 12 = Citizens. 8421836

Year 13 = Citizens. 8514476

Year 14 = Citizens. 8608135

Year 15 = Citizens. 8702825

Year 16 = Citizens. 8798556

What is missing here is the information about the ‘real years’ as 1 = 2016 etc.

SOURCE CODE for popShow0.py

popShow0.py as popShow0.pdf