output
Output:CA50_40_ref_data2101_E04_spec0-70 #0
Lifetimes (ns) : 0.4000 0.1250 2.0446
Std deviations : Fixed Fixed 0.0339
Intensities (%) : 69.2721 9.6726 21.0553
Std deviations : 1.0359 0.8128 0.4063
Time-zero Channel number : 41.5603
Std deviations : 0.0588
CA50_40_ref_data2101_E04_spec0-70 #1
Lifetimes (ns) : 0.4000 0.1250 2.0714
Std deviations : Fixed Fixed 0.0344
Intensities (%) : 70.0338 9.0952 20.8710
Std deviations : 1.0308 0.8135 0.4009
Time-zero Channel number : 41.5853
Std deviations : 0.0593
CA50_40_ref_data2101_E04_spec0-70 #2
Lifetimes (ns) : 0.4000 0.1250 2.0568
Std deviations : Fixed Fixed 0.0333
Intensities (%) : 69.5963 8.7445 21.6592
Std deviations : 1.0411 0.8177 0.4072
Time-zero Channel number : 41.5541
Std deviations : 0.0603
CA50_40_ref_data2101_E04_spec0-70 #3
Lifetimes (ns) : 0.4000 0.1250 2.0321
Std deviations : Fixed Fixed 0.0329
Intensities (%) : 70.4228 8.0614 21.5158
Std deviations : 1.0497 0.8219 0.4105
Time-zero Channel number : 41.4507
Std deviations : 0.0604
CA50_40_ref_data2101_E04_spec0-70 #4
Lifetimes (ns) : 0.4000 0.1250 2.0513
Std deviations : Fixed Fixed 0.0331
Intensities (%) : 67.2025 11.0731 21.7244
Std deviations : 1.0204 0.7976 0.4057
Time-zero Channel number : 41.6253
Std deviations : 0.0579
You may need to add some error checkingfrom itertools import zip_longest HEADER = ('Dataset Lifetimes Std deviations ' 'Intensities Std deviations ' 'Time-zero Std deviation\n') def grouper(iterable, n, fillvalue=''): "Collect data into fixed-length chunks or blocks" # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return zip_longest(*args, fillvalue=fillvalue) class Dataset: def __init__(self, line: str) -> None: self.data = line.strip().split()[1] def __repr__(self) -> str: return f'{self.data:7}' class LifeTimes: def __init__(self, line: str) -> None: self.data = line.strip().split()[3:6] def __repr__(self) -> str: return ' '.join(self.data) class StdDeviations: def __init__(self, line: str) -> None: split_line = line.strip().split() self.data = [split_line[num] if split_line[num:] else '' for num in range(3, 6)] def __repr__(self) -> str: return f"['{self.data[0]:^7}', '{self.data[1]:^7}', '{self.data[2]:^7}']" class Intensities: def __init__(self, line: str) -> None: self.data = line.strip().split()[3:6] def __repr__(self) -> str: return f'{self.data[0]:>7} {self.data[1]:>7} {self.data[2]:>7}' class TimeZero: def __init__(self, line: str) -> None: self.data = line.strip().split()[4] def __repr__(self) -> str: return f'{self.data:9}' class Block: def __init__(self, dataset, lifetimes, std_deviations, intensities, std_deviations2, time_zero, std_deviations3) -> None: self.dataset = Dataset(dataset) self.lifetimes = LifeTimes(lifetimes) self.std_deviations = StdDeviations(std_deviations) self.intensities = Intensities(intensities) self.std_deviations2 = StdDeviations(std_deviations2) self.time_zero = TimeZero(time_zero) self.std_deviations3 = StdDeviations(std_deviations3) def __repr__(self) -> str: return (f'{self.dataset} {self.lifetimes} {self.std_deviations}' f' {self.intensities} {self.std_deviations2}' f' {self.time_zero} {self.std_deviations3}\n') with open('output') as file_in, open('out', 'w') as file_out: file_out.write(HEADER) for group in grouper(file_in, 7): file_out.write(str(Block(*group)))out
Output:Dataset Lifetimes Std deviations Intensities Std deviations Time-zero Std deviation
#0 0.4000 0.1250 2.0446 [' Fixed ', ' Fixed ', '0.0339 '] 69.2721 9.6726 21.0553 ['1.0359 ', '0.8128 ', '0.4063 '] 41.5603 ['0.0588 ', ' ', ' ']
#1 0.4000 0.1250 2.0714 [' Fixed ', ' Fixed ', '0.0344 '] 70.0338 9.0952 20.8710 ['1.0308 ', '0.8135 ', '0.4009 '] 41.5853 ['0.0593 ', ' ', ' ']
#2 0.4000 0.1250 2.0568 [' Fixed ', ' Fixed ', '0.0333 '] 69.5963 8.7445 21.6592 ['1.0411 ', '0.8177 ', '0.4072 '] 41.5541 ['0.0603 ', ' ', ' ']
#3 0.4000 0.1250 2.0321 [' Fixed ', ' Fixed ', '0.0329 '] 70.4228 8.0614 21.5158 ['1.0497 ', '0.8219 ', '0.4105 '] 41.4507 ['0.0604 ', ' ', ' ']
#4 0.4000 0.1250 2.0513 [' Fixed ', ' Fixed ', '0.0331 '] 67.2025 11.0731 21.7244 ['1.0204 ', '0.7976 ', '0.4057 '] 41.6253 ['0.0579 ', ' ', ' ']