python-kurs-softed/beispiele/readfile4.py

25 lines
532 B
Python

#!/usr/bin/env python
import sys
filename = sys.argv[1]
data = {}
with open(filename, "r") as fileobj:
for i, line in enumerate(data):
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
for key in data:
print(key, data[key])