Liste des Groupes | Revenir à c misc |
Richard Kettlewell <invalid@invalid.invalid> wrote or quoted:Looks pretty neat and understandable to me. =)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}")
>
Les messages affichés proviennent d'usenet.