Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | D <nospam@example.net> |
|---|---|
| Newsgroups | comp.misc |
| Subject | Re: AWK As A Major Systems Programming Language |
| Date | 2024-08-19 19:42 +0200 |
| Organization | i2pn2 (i2pn.org) |
| Message-ID | <d3b29f68-3b98-7e9d-394b-d0aa81440d25@example.net> (permalink) |
| References | <slrnvc2fsg.gg1.bencollver@svadhyaya.localdomain> <awk-20240818120618@ram.dialup.fu-berlin.de> <66c26fda@news.ausics.net> <wwvr0alyn8k.fsf@LkoBDZeT.terraraq.uk> <awk-20240819124716@ram.dialup.fu-berlin.de> |
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}")
>
Back to comp.misc | Previous | Next — Previous in thread | Next in thread | Find similar
AWK As A Major Systems Programming Language Ben Collver <bencollver@tilde.pink> - 2024-08-18 00:28 +0000
Re: AWK As A Major Systems Programming Language not@telling.you.invalid (Computer Nerd Kev) - 2024-08-19 08:04 +1000
Re: AWK As A Major Systems Programming Language Richard Kettlewell <invalid@invalid.invalid> - 2024-08-19 08:40 +0100
Re: AWK As A Major Systems Programming Language Richard Kettlewell <invalid@invalid.invalid> - 2024-08-19 18:24 +0100
Re: AWK As A Major Systems Programming Language D <nospam@example.net> - 2024-08-19 19:42 +0200
Re: AWK As A Major Systems Programming Language not@telling.you.invalid (Computer Nerd Kev) - 2024-08-20 07:43 +1000
Re: AWK As A Major Systems Programming Language Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-18 23:36 +0000
Re: AWK As A Major Systems Programming Language Johanne Fairchild <jfairchild@tudado.org> - 2024-08-27 19:43 -0300
Re: AWK As A Major Systems Programming Language yeti <yeti@tilde.institute> - 2024-08-27 23:50 +0042
Re: AWK As A Major Systems Programming Language Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-27 23:36 +0000
Re: AWK As A Major Systems Programming Language Johanne Fairchild <jfairchild@tudado.org> - 2024-08-27 21:05 -0300
Re: AWK As A Major Systems Programming Language D <nospam@example.net> - 2024-08-28 16:25 +0200
Re: AWK As A Major Systems Programming Language Anton Shepelev <anton.txt@g{oogle}mail.com> - 2024-08-28 18:37 +0300
Re: AWK As A Major Systems Programming Language Johanne Fairchild <jfairchild@tudado.org> - 2024-08-30 18:56 -0300
Re: AWK As A Major Systems Programming Language D <nospam@example.net> - 2024-08-31 11:56 +0200
Re: AWK As A Major Systems Programming Language Johanne Fairchild <jfairchild@tudado.org> - 2024-08-31 12:28 -0300
Re: AWK As A Major Systems Programming Language D <nospam@example.net> - 2024-08-31 22:34 +0200
Re: AWK As A Major Systems Programming Language Johanne Fairchild <jfairchild@tudado.org> - 2024-09-01 22:52 -0300
Re: AWK As A Major Systems Programming Language Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-02 03:36 +0000
Re: AWK As A Major Systems Programming Language D <nospam@example.net> - 2024-09-02 09:45 +0200
Re: AWK As A Major Systems Programming Language Johanne Fairchild <jfairchild@tudado.org> - 2024-09-02 11:30 -0300
Re: AWK As A Major Systems Programming Language D <nospam@example.net> - 2024-09-02 18:31 +0200
Re: AWK As A Major Systems Programming Language Johanne Fairchild <jfairchild@tudado.org> - 2024-09-02 17:31 -0300
Re: AWK As A Major Systems Programming Language D <nospam@example.net> - 2024-09-02 23:08 +0200
Re: AWK As A Major Systems Programming Language yeti <yeti@tilde.institute> - 2024-09-02 23:15 +0042
Re: AWK As A Major Systems Programming Language D <nospam@example.net> - 2024-09-03 10:09 +0200
Re: AWK As A Major Systems Programming Language candycanearter07 <candycanearter07@candycanearter07.nomail.afraid> - 2024-09-02 23:50 +0000
Re: AWK As A Major Systems Programming Language Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-03 02:09 +0000
Re: AWK As A Major Systems Programming Language candycanearter07 <candycanearter07@candycanearter07.nomail.afraid> - 2024-09-05 15:10 +0000
Re: AWK As A Major Systems Programming Language D <nospam@example.net> - 2024-09-03 10:11 +0200
Re: AWK As A Major Systems Programming Language Anton Shepelev <anton.txt@g{oogle}mail.com> - 2024-08-21 19:18 +0300
Re: AWK As A Major Systems Programming Language Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-21 23:59 +0000
csiph-web