Apr-05-2022, 08:32 PM
This makes a dictionary where "Model" is a key and "Hamilton-C1" the value. You can use dictionary operations to get the keys or the values, or get the value associated with a key.
with open("data.txt", "r") as file: items = {} for line in file: if ":" in line: a, b = map(str.strip, line.split(":")) items[a] = b print(items) print(*items.keys()) print(*items.values())
Output:{'Model': 'Hamilton-C1', 'S/N': '25576', 'Export timestamp': '2020-09-17_11-03-40', 'SW-Version': '2.2.9'}
Model S/N Export timestamp SW-Version
Hamilton-C1 25576 2020-09-17_11-03-40 2.2.9
If you have no interest in a dictionary make items a list and append items.with open("data.txt", "r") as file: items = [] for line in file: if ":" in line: a, b = map(str.strip, line.split(":")) items.append(a) print(items)\
Output:['Model', 'S/N', 'Export timestamp', 'SW-Version']
If you want each item in the list to be a list do this.with open("data.txt", "r") as file: items = [] for line in file: if ":" in line: a, b = map(str.strip, line.split(":")) items.append([b]) print(items)
Output:[['Hamilton-C1'], ['25576'], ['2020-09-17_11-03-40'], ['2.2.9']]
If every line in you log file is in the form "name: value" you could open this as a CSV file using ":" as the delimiter, or you could read it using pandas.