Python join data lines together

2024/10/13 18:26:21

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.19RPM = -60.00
Time = 2 Temperature1 = 24.75 Temperature2 = 22.75 Temperature3 = 20.19RPM = -60.00
Time = 3 Temperature1 = 24.75 Temperature2 = 22.75 Temperature3 = 20.19RPM = -60.00
Time = 4 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.25RPM = -60.00
Time = 5 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.19RPM = -60.00
Time = 6 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.19RPM = -60.00
Time = 7 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.25RPM = -60.00
Time = 8 Temperature1 = 24.81 Temperature2 = 22.87 Temperature3 = 20.25RPM = -60.00
Time = 9 Temperature1 = 24.87 Temperature2 = 22.87 Temperature3 = 20.25RPM = -60.00
Time = 10 Temperature1 = 24.87 Temperature2 = 22.87 Temperature3 = 20.25RPM = -60.00
Answer

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

...

https://en.xdnf.cn/q/118051.html

Related Q&A

How to undraw plot with Zelle graphics?

This is a code problem for Python 3.5.2 using John Zelles graphics.py:I have spent a good amount of time looking for the answer here, but just can not figure it out. The function undraw() exists just l…

/bin/sh: line 62: to: command not found

I have a python code in which I am calling a shell command. The part of the code where I did the shell command is:try:def parse(text_list):text = \n.join(text_list)cwd = os.getcwd()os.chdir("/var/…

Using PyMySQL with MariaDB

I have read this article about switching from MySQL to MariaDB on Ubuntu. The article claims that it would be not problem to switch between the two. Currently, I am using PyMySQL to connect to my MySQL…

Overloading str in Python

I have code that looks like the following:class C(str):def __init__(self, a, b):print(init was called!)super().__init__(b)self.a = ac = C(12, c)When I try to run it, it gives me the following error:Tra…

Why cant I install Pillow on my mac? It gives some errors

here is the error from installing Pillow. Im using OS X Mavericks. I tried installing Pillow through pip install.._imaging.c:391:28: warning: implicit conversion loses integer precision: long to int [-…

what on earth the unicode number is?

in python:>>> "\xc4\xe3".decode("gbk").encode("utf-8") \xe4\xbd\xa0 >>> "\xc4\xe3".decode("gbk") u\u4f60we can get two conclusions:1.…

explicitly setting style sheet in python pyqt4?

In pyqt standard way for setting style sheet is like this:MainWindow.setStyleSheet(_fromUtf8("/*\n" "gridline-color: rgb(85, 170, 255);\n" "QToolTip\n" "{\n" &qu…

missing required Charfield in django is saved as empty string and do not raise an error

If I try to save incomplete model instance in Django 1.10, I would expect Django to raise an error. It does not seem to be the case.models.py:from django.db import modelsclass Essai(models.Model):ch1 =…

Beautiful soup missing some html table tags

Im trying to extract data from a website using beautiful soup to parse the html. Im currently trying to get the table data from the following webpage :link to webpageI want to get the data from the tab…

403 error Not Authorized to access this resource/api Google Admin SDK in web app even being admin

Im struggling to find the problem since two days without any idea why I get this error now even though the app was fully functional one month before.Among the tasks done by the web app, it makes an Adm…