File reading examples improvements

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
Christopher Arndt 2024-05-07 08:17:09 +02:00
parent a7cf6aed5f
commit 4c18aefce4
6 changed files with 81 additions and 3 deletions

View File

@ -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

5
beispiele/data.txt Normal file
View File

@ -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

View File

@ -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

View File

@ -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('#'):

29
beispiele/readfile5.py Normal file
View File

@ -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])

36
beispiele/readfile6.py Normal file
View File

@ -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])