Difference between revisions of "Python:Flexible Programming"

From PrattWiki
Jump to navigation Jump to search
(Created page with "This page covers some ways Python programs can be made more flexible by using strings and format specifiers. == Loading Various Files == If you have several files (in s...")
 
 
Line 1: Line 1:
 
This page covers some ways [[Python]] programs can be made more flexible by using strings and format specifiers.   
 
This page covers some ways [[Python]] programs can be made more flexible by using strings and format specifiers.   
 +
 +
== Loading Files With A Pattern ==
 +
If you have several files whose names fit a certain pattern (like data01.txt, data02.txt, etc) you can use Python's format command to build a string and then use <code>np.loadtxt</code> to load the file.  For instance, with the above pattern and, say, 8 data files with two columns of numbers each, you could access each file and print the average value of each column with:
 +
<syntaxhighlight lang=python>
 +
# %% Imports
 +
import numpy as np
 +
 +
# %% Loop to load data sets
 +
for k in range(8):
 +
    data = np.loadtxt("data{:02.0f}.dat".format(k + 1))
 +
    col1 = data[:,0].copy()
 +
    col2 = data[:,1].copy()
 +
    print("data{:02.0f}.dat column averages are {:0.2e} and {:0.2e}".format(k+1, col1.mean(), col2.mean()))
 +
 +
</syntaxhighlight>
  
 
== Loading Various Files ==
 
== Loading Various Files ==
 
If you have several files (in similar formats) that you need a script to load, rather than hard-coding each load, you can load the files in a loop.  There are two fundamentally different ways to go about this:
 
If you have several files (in similar formats) that you need a script to load, rather than hard-coding each load, you can load the files in a loop.  There are two fundamentally different ways to go about this:
 
* If the file names all have a pattern, you can use a formatted string to "build" the filename and then use that string with the appropriate loading command.
 
* If the file names all have a pattern, you can use a formatted string to "build" the filename and then use that string with the appropriate loading command.
* If the files are all ni the same folder, or in a folder where it is easy to hand-code files to exclude from loading, you can use the <code>os</code> module in Python and specifically the <code>os.listdir(PATH)</code> method to get a list of strings with the filenames at PATH.
+
* If the files are all in the same folder, or in a folder where it is easy to hand-code files to exclude from loading, you can use the <code>os</code> module in Python and specifically the <code>os.listdir(PATH)</code> method to get a list of strings with the filenames at PATH.
  
 
Here is a Trinket demonstrating the latter; note that Trinket does not have the ability to create folders so <code>main.py</code> and the two data files are all in the same place; the code on likes 12-13 causes the loop to skip past  
 
Here is a Trinket demonstrating the latter; note that Trinket does not have the ability to create folders so <code>main.py</code> and the two data files are all in the same place; the code on likes 12-13 causes the loop to skip past  

Latest revision as of 01:15, 1 September 2022

This page covers some ways Python programs can be made more flexible by using strings and format specifiers.

Loading Files With A Pattern

If you have several files whose names fit a certain pattern (like data01.txt, data02.txt, etc) you can use Python's format command to build a string and then use np.loadtxt to load the file. For instance, with the above pattern and, say, 8 data files with two columns of numbers each, you could access each file and print the average value of each column with:

# %% Imports
import numpy as np

# %% Loop to load data sets
for k in range(8):
    data = np.loadtxt("data{:02.0f}.dat".format(k + 1))
    col1 = data[:,0].copy()
    col2 = data[:,1].copy()
    print("data{:02.0f}.dat column averages are {:0.2e} and {:0.2e}".format(k+1, col1.mean(), col2.mean()))

Loading Various Files

If you have several files (in similar formats) that you need a script to load, rather than hard-coding each load, you can load the files in a loop. There are two fundamentally different ways to go about this:

  • If the file names all have a pattern, you can use a formatted string to "build" the filename and then use that string with the appropriate loading command.
  • If the files are all in the same folder, or in a folder where it is easy to hand-code files to exclude from loading, you can use the os module in Python and specifically the os.listdir(PATH) method to get a list of strings with the filenames at PATH.

Here is a Trinket demonstrating the latter; note that Trinket does not have the ability to create folders so main.py and the two data files are all in the same place; the code on likes 12-13 causes the loop to skip past any file names included in the list. If yuo aer able to put all your data files in a subfolder, that might work better - just adjust the path variable with the relative path to that subfolder from where the main script is running.

Also, this program shows two different ways of loading files: with pandas and with numpy. See the Pandas page for more specific information on how to load data using pandas.


Class Document Protection