Separate frequencies using Python and write values as many times (7895 7895 7895 7895) instead of (4*7895)

The below should work

with open('in.txt') as f:
    out_lines = []
    lines = [l.strip() for l in f.readlines()]
    for l in lines:
        parts = l.split()
        lst = []
        for part in parts:
            _parts = part.split('*')
            if len(_parts) == 1:
                lst.append(_parts[0])
            else:
                times = int(_parts[0])
                for i in range(times):
                    lst.append(_parts[1])
        out_lines.append(' '.join(lst))
with open('out.txt', 'w') as f1:
    for line in out_lines:
        f1.write(line + '\n')

in.txt

2*8.17997 723.188 4*33.33 3*11.0524 380.811 149.985 5*13.9643 22.8987 76.2205 2*24.7059 64.821
10*8.17997 723.188 4*33.33 3*11.0524 380.811 149.985 5*13.9643 22.8987 76.2205 2*24.7059 64.821

out.txt

8.17997 8.17997 723.188 33.33 33.33 33.33 33.33 11.0524 11.0524 11.0524 380.811 149.985 13.9643 13.9643 13.9643 13.9643 13.9643 22.8987 76.2205 24.7059 24.7059 64.821
8.17997 8.17997 8.17997 8.17997 8.17997 8.17997 8.17997 8.17997 8.17997 8.17997 723.188 33.33 33.33 33.33 33.33 11.0524 11.0524 11.0524 380.811 149.985 13.9643 13.9643 13.9643 13.9643 13.9643 22.8987 76.2205 24.7059 24.7059 64.821

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top