Re: AWK As A Major Systems Programming Language

Liste des GroupesRevenir à c misc 
Sujet : Re: AWK As A Major Systems Programming Language
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.misc
Date : 19. Aug 2024, 12:55:16
Autres entêtes
Organisation : Stefan Ram
Message-ID : <awk-20240819124716@ram.dialup.fu-berlin.de>
References : 1 2 3 4
Richard Kettlewell <invalid@invalid.invalid> wrote or quoted:
Yes, you could do a direct translation from the Awk and end up with
something that looked quite similar, apart from the differences in
syntax.

  Trying to make that Python script more user-friendly for folks
  who dig awk, then rolling out a sleeker version . . .

#!/usr/bin/env python3

import sys
import csv

# Initialize variables
total_price = 0
min_year = 0
max_year = 0
author_count = {}
year_count = {}

# Print the header of the report
print("Book Analysis Report")
print("====================")

# Read CSV data from standard input
reader = csv.reader(sys.stdin)
next(reader)  # Skip the header row

# Process each row in the CSV
for row in reader:
    title, author, year, price = row
    year = int(year)
    price = float(price)

    # Accumulate total price
    total_price += price

    # Determine min and max year
    if min_year == 0 or year < min_year:
        min_year = year
    if year > max_year:
        max_year = year

    # Count books per author
    if author in author_count:
        author_count[author] += 1
    else:
        author_count[author] = 1

    # Count books per year
    if year in year_count:
        year_count[year] += 1
    else:
        year_count[year] = 1

# Calculate total number of books
total_books = sum(author_count.values())

# Print the report
print("\nTotal number of books:", total_books)
print("Average book price: $%.2f" % (total_price / total_books))
print("Year range:", min_year, "to", max_year)

print("\nBooks per author:")
for author, count in author_count.items():
    print(author + ":", count)

print("\nBooks per year:")
for year, count in year_count.items():
    print(str(year) + ":", count)

  No need to make it such a big production, though . . .

import csv, sys
from collections import Counter

with open(sys.argv[1]) as f:
    data = list(csv.reader(f))[1:]
    prices = [float(row[3]) for row in data]
    years = [int(row[2]) for row in data]
    authors = [row[1] for row in data]

print("Book Analysis Report\n====================")
print(f"\nTotal number of books: {len(data)}")
print(f"Average book price: ${sum(prices) / len(prices):.2f}")
print(f"Year range: {min(years)} to {max(years)}")

print("\nBooks per author:")
for author, count in Counter(authors).items():
    print(f"{author}: {count}")

print("\nBooks per year:")
for year, count in Counter(years).items():
    print(f"{year}: {count}")

Date Sujet#  Auteur
18 Aug 24 * AWK As A Major Systems Programming Language36Ben Collver
18 Aug 24 +* Re: AWK As A Major Systems Programming Language10Stefan Ram
19 Aug 24 i+* Re: AWK As A Major Systems Programming Language7Computer Nerd Kev
19 Aug 24 ii`* Re: AWK As A Major Systems Programming Language6Richard Kettlewell
19 Aug 24 ii `* Re: AWK As A Major Systems Programming Language5Stefan Ram
19 Aug 24 ii  +* Re: AWK As A Major Systems Programming Language3Stefan Ram
19 Aug 24 ii  i+- Re: AWK As A Major Systems Programming Language1Richard Kettlewell
19 Aug 24 ii  i`- Re: AWK As A Major Systems Programming Language1Computer Nerd Kev
19 Aug 24 ii  `- Re: AWK As A Major Systems Programming Language1D
21 Aug 24 i`* Re: AWK As A Major Systems Programming Language2Anton Shepelev
22 Aug 24 i `- Re: AWK As A Major Systems Programming Language1Lawrence D'Oliveiro
19 Aug 24 `* Re: AWK As A Major Systems Programming Language25Lawrence D'Oliveiro
28 Aug 24  `* Re: AWK As A Major Systems Programming Language24Johanne Fairchild
28 Aug 24   +- Re: AWK As A Major Systems Programming Language1yeti
28 Aug 24   +* Re: AWK As A Major Systems Programming Language2Lawrence D'Oliveiro
28 Aug 24   i`- Re: AWK As A Major Systems Programming Language1Johanne Fairchild
28 Aug 24   `* Re: AWK As A Major Systems Programming Language20D
28 Aug 24    +- Re: AWK As A Major Systems Programming Language1Anton Shepelev
30 Aug 24    `* Re: AWK As A Major Systems Programming Language18Johanne Fairchild
31 Aug 24     `* Re: AWK As A Major Systems Programming Language17D
31 Aug 24      +* Re: AWK As A Major Systems Programming Language12Johanne Fairchild
31 Aug 24      i`* Re: AWK As A Major Systems Programming Language11D
2 Sep 24      i `* Re: AWK As A Major Systems Programming Language10Johanne Fairchild
2 Sep 24      i  +- Re: AWK As A Major Systems Programming Language1Lawrence D'Oliveiro
2 Sep 24      i  `* Re: AWK As A Major Systems Programming Language8D
2 Sep 24      i   `* Re: AWK As A Major Systems Programming Language7Johanne Fairchild
2 Sep 24      i    `* Re: AWK As A Major Systems Programming Language6D
2 Sep 24      i     `* Re: AWK As A Major Systems Programming Language5Johanne Fairchild
2 Sep 24      i      +* Re: AWK As A Major Systems Programming Language2Stefan Ram
3 Sep 24      i      i`- Re: AWK As A Major Systems Programming Language1D
2 Sep 24      i      +- Re: AWK As A Major Systems Programming Language1D
3 Sep 24      i      `- Re: AWK As A Major Systems Programming Language1yeti
3 Sep 24      `* Re: AWK As A Major Systems Programming Language4candycanearter07
3 Sep 24       +* Re: AWK As A Major Systems Programming Language2Lawrence D'Oliveiro
5 Sep 24       i`- Re: AWK As A Major Systems Programming Language1candycanearter07
3 Sep 24       `- Re: AWK As A Major Systems Programming Language1D

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal