Storing multiple data from arduino into a CSV file using python

I have no serious experience with python (I had to steal parts from your code and look up the rest) but with a little digging I came up with following (non-perfect) code

import serial
#import keyboard

try:
    ser = serial.Serial('COM4', baudrate=57600)
    ser.flushInput()

    while True:
        ser_bytes = ser.readline()
        print(ser_bytes)
        file = open("testfile.csv", "a")
        file.write(str(ser_bytes))
        file.close()
        
 #       if keyboard.is_pressed('esc'):
 #           break;
    ser.close
except:
    print("Unexpected error:", sys.exc_info()[0])
    print("Unexpected error:", sys.exc_info()[1])

The keyboard.is_pressed was an attempt to try to be able to interrupt the loop but it kept throwing an error.

Note that you might get rubbish with the first data (I suspect a remains of the bootloader); look at robin's example how he solved it.