Last active
January 1, 2016 17:49
-
-
Save Lysander/8179570 to your computer and use it in GitHub Desktop.
Lösung in Python 3. 3 zum Beitrag: http://forum.ubuntuusers.de/topic/sed-und-awk/ Hübsches Beispiel zur Anwendung von ``itertools.groupby`` :-)
Damit lassen sich Dateien mit klar getrennten Blöcken einfach parsen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Messung = 00-50-13dot95-1.dat | |
| Dateipfad = ...\...\ | |
| Kameraaufloesung [um] = 0.2 | |
| Sq [A] = 7 | |
| Sa [A] = 5 | |
| Messung = 00-50-13dot95-2.dat | |
| Dateipfad = ...\...\ | |
| Kameraaufloesung [um] = 0.2 | |
| Sq [A] = 21 | |
| Sa [A] = 5 | |
| Messung = 00-50-3dot95-1.dat | |
| Dateipfad = ...\...\ | |
| Kameraaufloesung [um] = 0.219295 | |
| Sq [A] = 6 | |
| Sa [A] = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> python transform_messdaten.py messdaten.txt | |
| Messung = 00-50-13dot95-1.dat | |
| Dateipfad = ...\...\ | |
| Kameraaufloesung [um] = 0.2 | |
| Sq [A] = 7 | |
| Sa [A] = 5 | |
| Zyklus = 00 | |
| Vergrößerung = 50 | |
| Winkel = 13dot95 | |
| Index= 1 | |
| Messung = 00-50-13dot95-2.dat | |
| Dateipfad = ...\...\ | |
| Kameraaufloesung [um] = 0.2 | |
| Sq [A] = 21 | |
| Sa [A] = 5 | |
| Zyklus = 00 | |
| Vergrößerung = 50 | |
| Winkel = 13dot95 | |
| Index= 2 | |
| Messung = 00-50-3dot95-1.dat | |
| Dateipfad = ...\...\ | |
| Kameraaufloesung [um] = 0.219295 | |
| Sq [A] = 6 | |
| Sa [A] = 4 | |
| Zyklus = 00 | |
| Vergrößerung = 50 | |
| Winkel = 3dot95 | |
| Index= 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # | |
| # Lösung in Python 3. 3 zum Beitrag: | |
| # http://forum.ubuntuusers.de/topic/sed-und-awk/ | |
| # | |
| # Hübsches Beispiel zur Anwendung von ``itertools.groupby`` :-) | |
| import re | |
| import sys | |
| import argparse | |
| from contextlib import closing | |
| from itertools import groupby | |
| TEMAPLTE = """Zyklus =\t {} | |
| Vergrößerung = \t {} | |
| Winkel = \t{} | |
| Index= \t {} | |
| """.strip() | |
| def get_data_blocks(rows): | |
| for key, group in groupby(rows, lambda s: s == "\n"): | |
| if not key: | |
| yield group | |
| def extract_values_from_first_line_of_block(row): | |
| return re.findall(r"(\d\d)-(\d\d)-(.+)-(\d)", row)[0] | |
| def expand_data_blocks(rows, template): | |
| for block in get_data_blocks(rows): | |
| rows = [row.strip() for row in block] | |
| extra_rows = template.format( | |
| *extract_values_from_first_line_of_block(rows[0])) | |
| yield "\n".join(rows) | |
| yield extra_rows | |
| yield "\n" | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Erweitert Messadten") | |
| parser.add_argument("messdaten", type=argparse.FileType('r'), | |
| help="Messdaten", default=sys.stdin) | |
| args = parser.parse_args() | |
| with closing(args.messdaten): | |
| print("\n".join(expand_data_blocks(args.messdaten, TEMAPLTE)), sep="") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment