Writing an Updating csv file with specific structure

Im doing some tests with spectrometer and want to create an updating csv file containing the results in python. im using pandas and the loop is working great, i was able to creat an updating file but im pretty new to python, and getting hard time creating this specific structure:

the first column will be the date and the local time with an empty cell in the first value. the first row ( starting from the second cell) will be a constant vector. ==> only first row. the second row and all others ( the first value will start from the second cell - first containing the date ) will be variable.

I need help defining this structure and conditions

The result of the code should shown in ecxel as the example picture (in the example there is only time, but i do need times and dates).

Thanks a lot !
Yuval

Image for reference:

image|690x388

Please show us what you’ve done/tried!

    time_tuple=logging.time.localtime()
    time_string=time.strftime("%m/%d/%Y, %H:%M:%S", time_tuple)
    fields=['wave length','intensity']
    with open(name, 'a') as f:
        write=csv.writer(f)
        write.writerow(time_string)
        write.writerow(fields)
        write.writerows(data)

data is a  1000x2 , the 2 columns are wave length and transmission

Hey bud , any suggestions? For getting the structure I mentioned?

what went wrong with this code you tried?

Hey Timo im trying to create a specific csv structure as in the picture , right now i’m trying to do so in 2 different scripts , as one creating the date column and wavelengths only ,that every wavelength will be in a separate column.
The other script adds the intensity of the wave to every wavelength column, to the same csv file with the date at first.

data containing the wave lengths and intensity in 2 different columns: data[:, 0] for wavelengths , and data[:, 1] for intensity

right now in the first script (that creates the wavelengths) , i’m getting only a shortcut string of the wavelength in the same tab, can’t understand why

Do you have any suggestions for solving this problem or trying a different method ? .

adding pictures of the 2 scripts and the csv file for the wavelengths.

Thanks alot for helping budd

cheers yuval

script2 => date&time and intensity

now = datetime.now()
time_string = now.strftime("%d/%m/%Y %H:%M:%S")
newData = [time_string, data[:, 1]]
with open(name, 'a') as f:
      write = csv.writer(f)
      write.writerow(newData)

csv file created from script1

script1 => wavelengths columns and ‘date’ title

for name in names:
    fields = ['date', data[:, 0]]
    with open(name, 'a') as f:
          write = csv.writer(f, delimiter=',')
          write.writerow(fields)

Your use of lists is a good fit for a csv file, but I think you should convert newdata to a string with appropriate field delimiters before writing to the file. Your screenshot shows the ‘[’ and ‘]’ that would be produced by str(a_list) or print(a_list)

Also, str(now) gives, e.g ‘2020-09-28 12:20:17.066763’, which is simpler than your approach.

If you don’t want the right digits, you could strip them using the str.rfind(’.’) method to locate the split point.