Python Script Hilfe

B

Bojan

Grünschnabel
Hey Leute,

bin ganz neu hier und auch das Programmieren fällt mir noch nicht ganz so leicht...
hab deswegen mal eine frage und hoffe jemand kann mir dabei helfen.
Ich habe 2 Messdatein in txt Form mit mehreren Spalten. Datei A ist um ein vielfaches länger als Datei B.
Nun möchte ich eine bestimmte Spalte von Datei B von einer anderen Bestimmten Spalte aus Datei A anziehen. Dabei soll sich die Spalte aus Datei B solange wiederholen bis ich ans Ende von Datei A angelangt bin
z. B. Datei A Spalte x Datei B Spalte y
A 1
B 2
C 1
D 2
E 1
F 2
G 1
H 2

Und am Ende brächte ich dann eine neue Datei mit den Spalten x , y, Differenz und Normalisierung der Differenz.

Ich hoffe Ihr könnt mir helfen.
Liebe Grüße
 
Hi Bojan,

willkommen im Forum! Kommt dieses Script dem nahe, was du willst. Du brauchst Python3 um es ausführen zu können! Die Normalisierung habe ich zwar nicht implementiert, aber das kannst du dann ja noch machen.

Code:
#!/usr/bin/python3

import sys

def read_data_from_file(filename):
    """Read the data from a text file where the columns are separated with a space."""
    rows = []
    with open(filename, "r") as fin:
        while True:
            line = fin.readline()
            if not line:
                break
            rows.append(line.rstrip().split(" "))
    return rows

def expand_by_cloning_rows(datalist, new_size):
    """Expand an existing list of data up to new_size by repeating the first lines."""
    ret = datalist.copy()
    pos = 0
    while len(ret) < new_size:
        ret.append(datalist[pos].copy())
        pos = (pos + 1) % len(datalist)
    return ret

def main(file1, file2):
    """The main function of the python script."""
    # We assume, that file1 is longer than file2 and file2 has to be expanded.
    data1 = read_data_from_file(file1)
    data2 = expand_by_cloning_rows(read_data_from_file(file2), len(data1))

    # Change these indices to your needs.
    index_1 = 1
    index_2 = 1

    print("Differences:")
    sum_diff = 0.0
    with open("results.dat", "w") as fout:
        for ii in range(len(data1)):
            diff = float(data1[ii][index_1]) - float(data2[ii][index_2])
            sum_diff = sum_diff + diff
            print("%s %s %f" % (data1[ii][index_1], data2[ii][index_2], diff), file=fout)
        print("Average: %f" % (sum_diff / len(data1)), file=fout)
    print("Results written to results.dat")
    return 0

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("ERROR: Too few files specified.", file=sys.stderr)
        print("Usage:")
        print(sys.argv[0] + " <file 1> <file 2>")
        sys.exit(1)
    sys.exit(main(sys.argv[1], sys.argv[2]))

Du kannst es genauer an deine Bedürfnisse anpassen. Ansonsten lohnt es sich wirklich, Python3 zu lernen - ganz besonders wenn du im wissenschaftlichen Umfeld tätig bist!

Viele Grüße,
Pik-9
 
Zuletzt bearbeitet:
Hallo Pik-9,
danke für deine Hilfe. Ja das werde ich müssen.
Habt ihr irgendwelche Empfehlungen wo ich es am besten lernen kann?

Viele Grüße
Bojan
 

Ähnliche Themen

Keine grafische Oberfläche (Debian Installation)

Bash - Zwei Binärdateien vergleichen (SQL Diff)

CSV Datei mit sed manipulieren/optimieren/ergänzen

Queue für copy Script

GUbutnu 14.04 LTS DualMonitor-Mode: Ubuntu merkt sich die Bildschirmpositionen nicht

Zurück
Oben