Writing logs to CSV file (Python, C language)

In my work, I had to take data continuously → write it to a CSV file and take a log. I tried to summarize it as a memorandum.

environment

OS:Raspbian Buster 10 Software used: Code-OSS (headmelted) 1.44.0

Program flow

Create an empty CSV file in the same hierarchy as the program and write the log to it. Add the date first, like "AD / month / day hour: minute: second, (data 1), (data 2) ..." List the data you want to record behind, separated by commas.

Python

csvWrite.py


import csv
import datetime

n = 0
try:
    with open('logger.csv', 'a') as f:
        #Write log 10 times
        while n < 10:
            #Date Time
            now = datetime.datetime.now()
            nowTime = '{0:%Y/%m/%d %H:%M:%S}'.format(now)

            #Data group
            data1 = 'data1'
            data2 = 'data2'
            data3 = 'data3'

            writeStr = nowTime + ',' + data1 + ',' + data2 + ',' +data3
            writeStr = writeStr.split(',')

            #Confirmation of the written character string
            #Example:'2020/03/06 15:00:00','data1','data2','data3'
            print('writeStr = %s' % writeStr)

            writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
            writer.writerow(writeStr)

            n += 1

except FileNotFoundError as e:
    print(e)
except csv.Error as e:
    print(e)

C language

csvWrite.c


#include <stdio.h>
#include <time.h>

FILE *fp;
char *fname = "logger.csv";
struct tm tm;

int main(void)
{
    struct tm tm;
    time_t t;

    //Data group
    char data1[] = "data1";
    char data2[] = "data2";
    char data3[] = "data3";

    //Open the file (if it doesn't exist, it will be created automatically)
    fp = fopen(fname, "a");
    if(fp == NULL){
        printf("file can't open¥n");
        return -1;
    }

    //Write log 10 times
    for(int n = 0; n < 10; n++){
        //Date Time
        t = time(NULL);
        localtime_r(&t, &tm);

        fprintf(fp, "\"%04d/%02d/%02d %02d:%02d:%02d\",",
            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
            tm.tm_hour, tm.tm_min, tm.tm_sec);
        fprintf(fp, "\"%s\",\"%s\",\"%s\"\n", data1, data2, data3);

        //Confirmation of the written character string
        //Example:"2020/03/06 15:00:00","data1","data2","data3"
        printf("writeStr = \"%04d/%02d/%02d %02d:%02d:%02d\",\"%s\",\"%s\",\"%s\"\n", 
            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
            tm.tm_hour, tm.tm_min, tm.tm_sec, data1, data2, data3);
    }

    fclose(fp);
    return 0;
}

Summary

When I am developing IoT, I leave a log on the server, but when I can not connect to the Internet The feeling of despair when the log disappears during that time is amazing, so I would like to create a CSV file on the SD card and save it diligently in this way.

Recommended Posts

Writing logs to CSV file (Python, C language)
[Python] Write to csv file with Python
Introduction to Protobuf-c (C language ⇔ Python)
Python CSV file reading and writing
[Python] Convert csv file delimiters to tab delimiters
How to read a CSV file with Python 2/3
Introduction to Python language
Read Python csv file
Try to make a Python module in C language
I tried to touch the CSV file with Python
How to convert JSON file to CSV file with Python Pandas
Python script to create a JSON file from a CSV file
Write to csv with Python
Download csv file with python
Call C language functions from Python to exchange multidimensional arrays
Python code for writing CSV data to DSX object storage
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
Writing C language with Sympy (metaprogramming)
How to wrap C in Python
Reading and writing CSV with Python
Python to switch from another language
Easy Python to learn while writing
File upload to Azure Storage (Python)
Call c language from python (python.h)
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
After calling the Shell file on Python, convert CSV to Parquet.
[Python] How to store a csv file as one-dimensional array data
C language to see and remember Part 2 Call C language from Python (argument) string
Closure 4 language comparison (Python, JavaScript, Java, C ++)
Read CSV file with python (Download & parse CSV file)
How to convert Python to an exe file
Write O_SYNC file in C and Python
C language to see and remember Part 1 Call C language from Python (hello world)
I want to analyze logs with Python
[Python] How to read a csv file (read_csv method of pandas module)
Read Python csv and export to txt
Socket communication by C language and Python
[Part1] Scraping with Python → Organize to csv!
Python> Output numbers from 1 to 100, 501 to 600> For csv
Generate C language from S-expressions in Python
Read CSV file with Python and convert it to DataFrame as it is
Python script that outputs all records of Oracle table to CSV file
C language to see and remember Part 4 Call C language from Python (argument) double
Error due to UnicodeDecodeError when reading CSV file with Python [For beginners]
An introduction to Python for C programmers
C language to see and remember Part 5 Call C language from Python (argument) Array
Various ways to read the last line of a csv file in Python
[Python] Scan the inside of the folder including subfolders → Export the file list to CSV
I made a module in C language to filter images loaded by Python
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
How to use the C library in Python
Scraping tabelog with python and outputting to CSV
Python C / C ++ Extension Pattern-Pass data to Python as np.array
Python inexperienced person tries to knock 100 language processing 14-16
How to generate permutations in Python and C ++
[Python] Convert from DICOM to PNG or CSV
Import Excel file from Python (register to DB)
Ported a naive homebrew language compiler to Python
From file to graph drawing in Python. Elementary elementary
How to multi-process exclusive control in C language
[Python] How to read excel file with pandas