Skip to content Skip to sidebar Skip to footer

Python Join Data Lines Together

Hello i have dataset a few thousand lines which is split in even and odd number lines and i can't find a way to join them together again in the same line. Reading the file and over

Solution 1:

You can use % (modulus) to determine if the line is odd or even. If it's even, then join together the last line and the current line.

# Using your dataset as a string
data_split = data.split("\n")

for i in range(len(data_split)):
    if i % 2:
        lines = [data_split[i-1], data_split[i]]
        print" ".join(lines)

Output:

Time = 1 Temperature1 = 24.75 Temperature2 = 22.69 Temperature3 = 20.19 RPM = -60.00

Time = 2 Temperature1 = 24.75 Temperature2 = 22.75 Temperature3 = 20.19 RPM = -60.00

Time = 3 Temperature1 = 24.75 Temperature2 = 22.75 Temperature3 = 20.19 RPM = -60.00

...

Post a Comment for "Python Join Data Lines Together"