Apr-06-2022, 08:18 PM
(Apr-05-2022, 08:32 PM)deanhystad Wrote: 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())If you have no interest in a dictionary make items a list and append items.
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
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)\
If you want each item in the list to be a list do this.
Output:['Model', 'S/N', 'Export timestamp', 'SW-Version']
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)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.
Output:[['Hamilton-C1'], ['25576'], ['2020-09-17_11-03-40'], ['2.2.9']]
Thank you for helping. For me is the Output of only the values (last solution) the best.
But If I execute the code, I got the following error:
Output:---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [167], in <module>
5 for line in file:
6 if ":" in line:
----> 7 a,b = map(str.strip, line.split(":"))
8 items.append([b])
10 print(items)
ValueError: too many values to unpack (expected 2)
How I can deal with this?