0

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 overwriting it is fine or making a new file.

I have found this example to print the seperate lines but can't get it to write it to file.

I would like it to look like this:

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
etc...

Example of dataset:

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
Time = 4 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.25
 RPM = -60.00
Time = 5 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.19
 RPM = -60.00
Time = 6 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.19
 RPM = -60.00
Time = 7 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.25
 RPM = -60.00
Time = 8 Temperature1 = 24.81 Temperature2 = 22.87 Temperature3 = 20.25
 RPM = -60.00
Time = 9 Temperature1 = 24.87 Temperature2 = 22.87 Temperature3 = 20.25
 RPM = -60.00
Time = 10 Temperature1 = 24.87 Temperature2 = 22.87 Temperature3 = 20.25
 RPM = -60.00
Community
  • 1
  • 1

1 Answers1

0

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

...

Green Cell
  • 4,677
  • 2
  • 18
  • 49
  • Yeah i can see that it works as a string but when i import my file then the split command doesn't work with file type, then it won't print a thing out. And i am not sure how to write to a file from an IF statement. (Am new to python) – Gísli Trausti Jóhannesson Sep 30 '16 at 08:29
  • Managed to read the file into str variable with [code]data=myfile.read() – Gísli Trausti Jóhannesson Sep 30 '16 at 08:37
  • Got it to work with your code: `with open('LOGT.txt', 'r') as myfile: data=myfile.read() data_split = data.split("\n") for i in range(len(data_split)): if i % 2: lines = [data_split[i-1], data_split[i]] joined = " ".join(lines) fh = open("LOGT1.txt","a") fh.write(joined) fh.write("\n") fh.close()` – Gísli Trausti Jóhannesson Sep 30 '16 at 08:46
  • Or you can use `readline()` instead and check if the line is odd or even. Glad you got something working though. – Green Cell Oct 01 '16 at 10:05