Recording data in python

I am reading humidity sensor data with Arduino and Python. I want to store data in Python and I can do it but the file is not sorted. I can't use data without order. Which is the reason? I have tried making changes and it does not maintain the order to use it in a spreadsheet.
I add the program code and storage file.
import time,serial

Arduino= serial.Serial("/dev/ttyACM0","9600",timeout=0)

time.sleep (2)

while True:

Lectura=Arduino.readline().strip().decode("utf-8")

if len(Lectura)>0:
	with open('Medicion.csv','a', newline="") as Archivo:
	                    
		Archivo.write(f'{Lectura}\n')
                                 			
	print("Data appended")
                               
else:
	print("No Data appended")


Arduino code

#include "DHT.h"

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

const int DHTPin = 5;     // what digital pin we're connected to

DHT dht(DHTPin, DHTTYPE);

void setup() {
  Serial.begin(9600);
  // Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(10000);

  // Reading temperature or humidity takes about 250 milliseconds!
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Serial.print("Humidity: ");
  Serial.print(h);
   Serial.print(",");
  //Serial.print("\t");
  // Serial.print(" %\t");
  // Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println();
  // Serial.print(" *C ");
}

56.6
0,18
.7018.70
56
.60,
18.7
018.
70
56.6
0,18
.701
8.70
56
.60
,18.
7018
.70

The data are in the exact order in which they were written by your program.

Some planning is required when storing data. For example, how are the individual measurements supposed to be identified by a program reading the file?

Is it important to know at what time the data were collected and written?

I want to have the pair of values ​​T, H separately, following a sequence that allows working in a spreadsheet

Write out the two values per line, with a comma separating them. That is "CSV" format (comma separated values), and most spreadsheets can read it.

Consult the Python language documentation to learn how to do that.

I have used csv format,you can see it in the code. I can probe, what you say, but I have made different probes

Change the first to a println(t); So you get a cr after the "t" data.

The second line is printing NOTHING, so the compiler ignores it.

The data SHOWN are not in .csv format. What produced that listing? The file extension ".csv" is irrelevant.

Why is Python being discussed here?

What has this code to do with Python? It is correct and will produce one line of comma separated values.

  Serial.print(h);
   Serial.print(",");
  //Serial.print("\t");
  // Serial.print(" %\t");
  // Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println();

well, I will probe that, I will answer you.

Not true. It prints a line ending.

Serial.println();

No. With the void argument it is printing "\r\n"

size_t Print::println(void)
{
  return write("\r\n");
}

cores/arduino/Print.cpp

On the python side, have you separated the issues between reading in the data and storing it in a file? When you find where your problem lies you can focus your efforts.

If true, the OP would net be having problems.

No file closing?

do you say? : Arduino.close() . The code is working all the time with While (true)
If I close serial port, I would need to open it every time in While.

I have been looking at the python end of this program, and I am finding that Archivo.close() is not required for the data being saved in the file.

I'm not certain what issuses @sonntag94 is having, but when I send data as the two comma separated floats and the .println( ) terminator from the Arduino to python on a PC I can read the saved file.

Serial.print(h);
Serial.print(",");
Serial.print(t);
Serial.println();
import serial
import time

Arduino= serial.Serial("COM5","9600",timeout=0)

time.sleep (2)

while True:

    Lectura=Arduino.readline().strip().decode("utf-8")
    if(Lectura):
        print("Data Read:")
        print(Lectura)

        if len(Lectura)>0:
            with open('Medicion.csv','a', newline="") as Archivo:
	                    
                Archivo.write(f'{Lectura}\n')
                                 			
            print("Data appended")

        else:
            print("No Data appended")

If it's going into a spreadsheet, why not let the spreadsheet do the sorting?

The data is cut, a part of the temperature is written next to the humidity, you can see it in what I showed. I don't think a spreadsheet can solve that, or understand it.

Try running this simple program on the Arduino with the python code I posted in #15.

void setup() {
  Serial.begin(9600);

}

void loop() {
  // Wait a few seconds between measurements.
  delay(3000);

  // Reading temperature or humidity takes about 250 milliseconds!
  static float h = 10.0;
  static float t = 20.0;

  Serial.print(h);
  Serial.print(",");
  Serial.print(t);
  Serial.println();
  h += .1;
  t += .1;

}

The data in Medicion.csv looks like this

10.00,20.00
10.10,20.10
10.20,20.20
10.30,20.30
10.40,20.40
10.50,20.50

well I will probe it. I would like to know what action this does: ```
h += .1;
t += .1;

It increments the number being sent so that it mimics changing data.