From 4c18aefce47566b23a76d480330f25cc84d338aa Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Tue, 7 May 2024 08:17:09 +0200 Subject: [PATCH] File reading examples improvements Signed-off-by: Christopher Arndt --- beispiele/data-errors.txt | 5 +++++ beispiele/data.txt | 5 +++++ beispiele/readfile3.py | 2 +- beispiele/readfile4.py | 7 +++++-- beispiele/readfile5.py | 29 +++++++++++++++++++++++++++++ beispiele/readfile6.py | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 beispiele/data-errors.txt create mode 100644 beispiele/data.txt create mode 100644 beispiele/readfile5.py create mode 100644 beispiele/readfile6.py diff --git a/beispiele/data-errors.txt b/beispiele/data-errors.txt new file mode 100644 index 0000000..8f28c8f --- /dev/null +++ b/beispiele/data-errors.txt @@ -0,0 +1,5 @@ +record 1: 100, 300, 50, 200 +record 2 300, 450, 50, 100 +record 3: 150, 200, 0, 150 +record 4: 350, 200, 5O, 250 +record 5: 50, 100, 200, 300 diff --git a/beispiele/data.txt b/beispiele/data.txt new file mode 100644 index 0000000..f6e2757 --- /dev/null +++ b/beispiele/data.txt @@ -0,0 +1,5 @@ +record 1: 100, 300, 50, 200 +record 2: 300, 450, 50, 100 +record 3: 150, 200, 0, 150 +record 4: 350, 200, 50, 250 +record 5: 50, 100, 200, 300 diff --git a/beispiele/readfile3.py b/beispiele/readfile3.py index 12d0b20..3cc4d7d 100644 --- a/beispiele/readfile3.py +++ b/beispiele/readfile3.py @@ -8,7 +8,7 @@ with open(filename, "r") as fileobj: max_linelength = 0 longest_line = -1 - for i, line in enumerate(data): + for i, line in enumerate(fileobj): if len(line) > max_linelength: max_linelength = len(line) longest_line = i + 1 diff --git a/beispiele/readfile4.py b/beispiele/readfile4.py index c942e33..93fc8eb 100644 --- a/beispiele/readfile4.py +++ b/beispiele/readfile4.py @@ -2,12 +2,15 @@ import sys -filename = sys.argv[1] +if len(sys.argv) >= 2: + filename = sys.argv[1] +else: + filename = "data.txt" data = {} with open(filename, "r") as fileobj: - for i, line in enumerate(data): + for i, line in enumerate(fileobj): line = line.strip() # remove whitespace from start/end of line if line.startswith('#'): diff --git a/beispiele/readfile5.py b/beispiele/readfile5.py new file mode 100644 index 0000000..e8bb99e --- /dev/null +++ b/beispiele/readfile5.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +import sys + +if len(sys.argv) >= 2: + filename = sys.argv[1] +else: + filename = "data.txt" + +data = {} + +try: + with open(filename, "r") as fileobj: + for i, line in enumerate(fileobj): + line = line.strip() # remove whitespace from start/end of line + + if line.startswith('#'): + # ignore comment lines + continue + + name, raw_data = line.split(":", 1) # split line at first colon + items = raw_data.split(",") # split raw data at commas + + data[name.strip()] = items +except OSError as exc: + print(f"Could not open file {filename}: {exc}") + +for key in data: + print(key, data[key]) diff --git a/beispiele/readfile6.py b/beispiele/readfile6.py new file mode 100644 index 0000000..efabef5 --- /dev/null +++ b/beispiele/readfile6.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +import sys + +if len(sys.argv) >= 2: + filename = sys.argv[1] +else: + filename = "data.txt" + +data = {} + +try: + with open(filename, "r") as fileobj: + for i, line in enumerate(fileobj): + line = line.strip() # remove whitespace from start/end of line + + if line.startswith('#'): + # ignore comment lines + continue + + try: + name, raw_data = line.split(":", 1) # split line at first colon + except ValueError as exc: + print(f"Warning: could not parse line {i+1}: {exc}") + else: + try: + items = [int(item) for item in raw_data.split(",")] # split raw data at commas + except (ValueError, TypeError) as exc: + print(f"Warning: could not parse data on line {i+1}: {exc}") + + data[name.strip()] = items +except OSError as exc: + print(f"Could not open file {filename}: {exc}") + +for key in data: + print(key, data[key])