DeaD_EyE

csv kram ...

Jul 14th, 2020
57
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. Beschreibung ...
  3. """
  4.  
  5. import csv
  6. import sys
  7. from argparse import ArgumentParser, FileType
  8. from datetime import datetime as DateTime
  9. from urllib.request import urlretrieve
  10.  
  11.  
  12. def get_demo_data():
  13.     csv_url = "https://opendata.ecdc.europa.eu/covid19/casedistribution/csv"
  14.     file, http_reply = urlretrieve(csv_url)
  15.     return file
  16.  
  17.  
  18. def constraints(row):
  19.     """
  20.    Filter nach Fälle > 100
  21.    """
  22.     try:
  23.         return int(row[4]) > 100 and row[6].casefold() == "germany"
  24.     except ValueError:
  25.         return False
  26.  
  27.  
  28. def transformer(row):
  29.     """
  30.    Nur Datum, Sterbefälle und Land ausgeben.
  31.    """
  32.     try:
  33.         return [DateTime.strptime(row[0], "%d/%m/%Y").date().isoformat(), row[4], row[6]]
  34.     except ValueError:
  35.         return [row[0], row[4], row[6]]
  36.  
  37.  
  38. def main(input_file, output_file):
  39.     with open(input_file, "rt", encoding="utf8", newline="") as fd_in:
  40.         reader = csv.reader(fd_in)
  41.         with open(output_file, "wt", encoding="utf8", newline="") as fd_out:
  42.             writer = csv.writer(fd_out)
  43.             # den header schreibe ich explizit
  44.             # der transformer selektiert spalten
  45.             writer.writerow(transformer(next(reader)))
  46.             for row in reader:
  47.                 if constraints(row):
  48.                     row = transformer(row)
  49.                     writer.writerow(row)
  50.  
  51.  
  52. def get_args():
  53.     parser = ArgumentParser(__doc__)
  54.     parser.add_argument("input_file", help="CSV Eingabedatei")
  55.     parser.add_argument("output_file", help="CSV Ausgabedatei")
  56.     return parser.parse_args()
  57.  
  58.  
  59. if __name__ == "__main__":
  60.     main(get_demo_data(), "output.csv")
  61. #    args = get_args()
  62. #    try:
  63. #        main(**vars(args))
  64. #    except Exception as e:
  65. #        print(repr(e))
  66. #        sys.exit(1)
RAW Paste Data