Re: AWK As A Major Systems Programming Language

Liste des GroupesRevenir à c misc 
Sujet : Re: AWK As A Major Systems Programming Language
De : nospam (at) *nospam* example.net (D)
Groupes : comp.misc
Date : 19. Aug 2024, 18:42:10
Autres entêtes
Organisation : i2pn2 (i2pn.org)
Message-ID : <d3b29f68-3b98-7e9d-394b-d0aa81440d25@example.net>
References : 1 2 3 4 5
On Mon, 19 Aug 2024, Stefan Ram wrote:

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 . . .
Looks pretty neat and understandable to me. =)

#!/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