csv and html from Python

Table of Contents

CSV

  1. The Python Standard Library has a module for reading and writing csv files. csv stands for Comma-separated values. It's a straightforward way for writing tabular data. You can export spreadsheets as a .csv file.
  2. Let's generate some random grades for a fake assignment

    import csv
    import random
    
    STUDENTS_1 = [
        "hannah",
        "ilai",
        "emma",
        "marta",
        "jai yoon",
        "tim",
        "gresa",
        "janine",
        "kurt",
        "kangning",
        "victor",
        "parthivi",
        "per",
        "maria",
        "philip",
        "georgy",
        "iacob",
        "nacho",
        "lea",
        "sarah",
        "alicja",
        "anna",
        "larisa",
        "tessa",
    ]
    
    STUDENTS_2 = [
        "tanmaya",
        "louisa",
        "kayna",
        "théo",
        "patrick",
        "miranda",
        "anthony",
        "kahlil",
        "leonie",
        "makss",
        "alix",
        "caroline",
        "milla",
        "daria",
        "merel",
        "katerina",
        "orianna",
        "naranbat",
        "neram",
        "grazia",
        "lesley",
        "moeka",
        "ingrid",
    ]
    
    STUDENTS = STUDENTS_1 + STUDENTS_2
    
    GRADEFILE_NAME = "/home/bon/tmp/fake_grades.csv"
    
    
    def random_grade():
        return int(50 + random.uniform(0, 50))
    
    
    def write_fake_grades():
        with open(GRADEFILE_NAME, "w") as grade_file:
            writer = csv.writer(grade_file)
            header = ["student"]
            for a in range(1, 9):
                header.append("Assignment {}".format(a))
            for e in range(1, 4):
                header.append("Exam {}".format(e))
            writer.writerow(header)
            for student in STUDENTS:
                grade_line = [student]
                for g in range(11):
                    grade_line.append(random_grade())
                writer.writerow(grade_line)
    

    When we call write_fake_grades(), the file gets written.

  3. Now we add a function to read in the grades for the first assignment and write them to screen in a nice format.

    def read_fake_grades():
        with open(GRADEFILE_NAME, "r") as csv_file:
            reader = csv.reader(csv_file)
            for n, row in enumerate(reader):
                if n == 0:
                    continue
                student, percent, *rest = row
                print("{:>12} {:>3}".format(student, percent))
    

News feeds

Some news, form and blog web sites publish a list of articles in the form of a Rich Site Summary or RSS feed. We can use the Python feedparser module to access these feeds and make our own personal news summary. With the Natural Language Toolkit Python library the possibilities for mixing and matching are endless. See feeds.py

HTML

  1. And how about reading html? There is a huge amount of information on the Web written in html format. Beautiful Soup is one of many Python libraries for "screen scraping".

Author: Breanndán Ó Nualláin <o@uva.nl>

Date: 2025-09-04 Thu 08:55