micropython logging to CSV



  • Hi there,

    I am working on a device which suppose to log values, and I want to store the result in CSV format, for easier handling from code and for the future.
    I found a handy python library for that (csv), but VS cannot import that library. Is it possible that csv library has not been ported to micropython? Can you recommend me a solution or a workaround? Am I doing it the "right way" or there is some other best practices?
    Thanks,

    https://docs.python.org/2/library/csv.html



  • @eric73 I am just creating the file and the first row, in my mind that's belongs to initialization. But correct me if not.
    Okay than I just use the basic file operations.
    Thank you! :)



  • @tttadam boot.py is not the rigth place to have some code running, keep this file only for startup board setting (bluetooth and wifi) and place your code in main.py or other file (not mandatory but a good practice).

    CSV is no more than several ascii string line write i a file with a separator (popular is ',' or ';') between value. CVS module from python provide a lot of helper function to do advance stuff on CSV file, if you only :
    Write first line of your file with name colomn of your field (ex: Time,temp,Sensor name) (not mandatory but a good practive to name your field for post-processing)
    Then write the followings lines with value
    1538988400,25.2,T1
    1538988401,25.1,T1
    1538988401,25.3,T1
    ...
    Then you have a perfectly valid CSV file you can open in PC with OpenOffice Calc for exemple.



  • @eric73 Thank you for making it clear for me. My intention was never to store the data on flash.
    I forget the mention that I will store the data on sd card. I think there is a plenty of space there.

    This is error what I get:

    Traceback (most recent call last):
      File "boot.py", line 3, in <module>
    ImportError: cannot import name csv


  • @tttadam CSV is unefficient way to store data
    ex: to store the value 100 you use 3 octet plus a comma sign as separator (4 bytes to store a value that fit in one byte!)
    In embedded system memory (even for storage) is expensive !
    The better way to store your value is to pack it in binary form (using ustruct micropython module) and write this binary package in your file.
    You can simply on PC side, write a little software in python that unpack your embedded file to csv



Pycom on Twitter