Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Computer Lab 08, files

Count characters, words, and lines in a file

In module texttools.py, create a function count() that will return a triple consisting of the number of characters (including white space, but excluding new-line characters), words, and lines in a text file, given to the function as an argument.

def count(filepath):
    """Return number of characters, words, and lines in the given file
 
    :param filepath: string, path to a text file
    :return: 3-tuple with number of chars, words, and lines in the file
    """

If we had a file named example.txt in the current directory with the following contents

One two three,
dee dah dee.
Four, five, six,
what a mix!

we could execute the code and get results like this:

>>> print(count('example.txt'))
(53, 12, 4)

Inserting line numbers

In module texttools.py, create function insert_line_numbers() which shall read the input text file line by line, and write them into an output text file such that each line will start with an n-digit long line number and a space, starting from 1. The default value for n is 3.

def insert_line_numbers(in_filepath, out_filepath, num_len=3):
    """Create a copy of the input file with numbered lines
 
    :param in_filepath: string, path to the input text file
    :param out_filepath: string, path to the output text file
    :param num_len: int, the length of the string containg the line number
    :return: None
    """

Running the function on the text file from the previous exercise like this:

>>> insert_line_numbers('example.txt', 'numbered_example.txt', num_len=2)

will create file numbered_example.txt in the current directory with the following contents:

 1 One two three,
 2 dee dah dee.
 3 Four, five, six,
 4 what a mix!

Try to apply this function to some of your .py files!

Removing the line beginnings

In module texttools.py, create function snip_line_beginnings() that can be used to undo the line numbering (but can be used in more general cases).

def snip_line_beginnings(in_filepath, out_filepath, n=4):
    """Create a copy of the input file with the first n chars of each line snipped off.
 
    :param in_filepath: string, path to the input text file
    :param out_filepath: string, path to the output text file
    :param n: int, the number of chars to snip from the beginning of each line
    :return: None
    """

Weekly Homework 08 - Chemical elements database

Let's have a file with a simple database of chemical elements, named e.g. elements.txt, with the following contents:

berryllium 4 9.012
magnesium 12 24.305
calcium 20 20.078
strontium 38 87.62
barium 56 137.327
radium 88 226

For each stored chemical element, its respective line in a file contains the element name, atomic number and atomic weight.

Write function read_elements() that will accept a path to the file with element database, will read the information therein and will produce a list of tuples, where each tuple will contain data for an individual element, i.e. its name (string), its atomic number (int), and atomic weight (float).

def read_elements(filename):
    """Return a list of lists with info about chem. elements read from the given file
 
    :param filename: string, path to a file with the elements database
    :return: list of tuples, each tuple containing name (string), number (int),
             and weight (float) for each element.
    """ 

When the function is used to read in the above file, it should produce the following:

>>> read_elements('elements.txt')
[('berryllium', 4, 9.012), ('magnesium', 12, 24.305), ('calcium', 20, 20.078), ...]
Required filename: 08_weekly_hw.py.

homework

Solve homework 04 - Files and submit it via Upload system in time! Check the deadline in Upload system.
courses/be5b33prg/labs/week_08.txt · Last modified: 2020/11/12 14:30 by nemymila