简体   繁体   中英

Continues columns in a csv-file with python

I have a Problem with continues writing my datas in a csv-file. I want a program that detects, if there is a csv-file for my measurements-data. If not it would be generated. When the csv-file is new generated the datas are written in the csv-file on the column after the header with the variable cycle = 0 .

If the csv-file exists, the datas should be written continuously after the last line of the csv. Also the variable cycle should continue.

I have written a program that can detect if there is a file or not but with the continuously lines I have problems. I hope someone can help me.

# mes = Array with 20 spaces filled with the Numbers 0-19

date = time.strftime("%d/%m/%Y")

def write(cycle, mes):

    if os.path.exists('/home/pi/Documents/Ventilatorprüfstand_Programm/out.csv') is True: #does the out.csv existate?
        print("Do something")
        out = open('out.csv', 'w')
        data = [[cycle, mes[0],mes[1],mes[2],mes[3],mes[4],mes[5],mes[6],mes[7],mes[8],mes[9],mes[10],mes[11],mes[12],mes[13],mes[14],mes[15],mes[16],mes[17],mes[18],mes[19], date]]
        line = cycle+1    
        for row in data:
            for line in row:
                out.write('%s;' % line)
            out.write('\n')   
        out.close()

    else:
        print("Do another something")
        header = lookuptable.names()
        out = open('out.csv', 'w')
        for row in header:
            for column in row:
                out.write('%s' % column)
            out.write('\t')
        out.write('\n')

        data = [[cycle, mes[0],mes[1],mes[2],mes[3],mes[4],mes[5],mes[6],mes[7],mes[8],mes[9],mes[10],mes[11],mes[12],mes[13],mes[14],mes[15],mes[16],mes[17],mes[18],mes[19], date]]


        for row in data:
            for column in row:
                out.write('%s;' % column)
            out.write('\n')
        out.close()`

When opening the file with open() there is the option 'a' to append the new lines to the end:

'a' open for writing, appending to the end of the file if it exists

Here is an example using the csv Python standard library:

import csv
import os
import random

headers = ['cycle', 'date', 'speed', 'temp', 'power']

new_data = [[random.randint(0, 100) for _ in range(3)] for _ in range(2)]
date = '00/01/02'
cycle = 1

#  Copy the data and include the date and the cycle number:
full_rows = [ [cycle, date, *row] for row in new_data ]

filename = 'example.csv'

# Check if the file exist, if not create the file with header
if not os.path.exists(filename):
    print('creating a new file')
    with open(filename, 'w') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',')
        csvwriter.writerow(headers)  # add the header

# Append the data to the file
with open(filename, 'a', newline='') as csvfile:  # note the 'a' option
    csvwriter = csv.writer(csvfile, delimiter=',')
    csvwriter.writerows(full_rows)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM