Posts tagged ‘AWK’

Spalte aus Text-Datei extrahieren mit Python

Hier ein Quick & Dirty Code, um eine Spalte aus einer Text-Datei zu extrahieren. Geht auch mit AWK, aber wenn man nur Python hat…

def splitFileOneColumn(inputFile,outputFile,columnSeparator,column):
    with open(inputFile, 'r') as infile:
        with open(outputFile, 'w') as outfile:
            for line in infile:
                s = line.split(columnSeparator)
                outfile.write(s[column]+os.linesep) # '\r\n' on Windows, '\n' on Unix/Linux/Mac
            outfile.close()
    infile.close()

Bei Gelegenheit muss ich das mal um die Möglichkeit erweitern, n Spalten zu extrahieren.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website