Shop OBEX P1 Docs P2 Docs Learn Events
Create a dot csv without a 3rd party module — Parallax Forums

Create a dot csv without a 3rd party module

UnsoundcodeUnsoundcode Posts: 1,530
edited 2023-12-31 16:11 in micro:bit

This is an example of creating a csv file on the microbit flash that can be opened on a PC with Excel or Open Office Calc without the need for any additional module import, I used the random module to create some data other than that it would not be needed.

The program creates a list of lists which includes a header and data, after running the code use Thonny in Windows to refresh the flash view and right click on the my.csv file where there is an option to download the file to the user folder on your PC and from there you can right click the file and open with the default app which in my case is Excel.

from microbit import *
import random

my_list = []

f = open('my.csv','w')

my_header ='Value 1' + ',' + 'Value 2' + ',' + 'Value 3' + '\n'
my_list.append(my_header)

f.write(my_list[0])

for i in range(1,20):
    my_list.append([])
    val_1 = random.randint(0, 10)
    val_2 = random.randint(0, 10)
    val_3 = random.randint(0, 10)
    values= str(val_1) + ',' + str(val_2) + ',' + str(val_3) + '\n'
    my_list[i] = values
    f.write(my_list[i])

f.close()

Comments

  • I ran your code snippet, it ran without errors, but I could not find the created .csv file. Is still being opened in the Downloads folder?

    Ray

  • JonnyMacJonnyMac Posts: 8,894
    edited 2023-12-31 22:18

    I ran your code snippet, it ran without errors, but I could not find the created .csv file. Is still being opened in the Downloads folder?

    No. It is located on the micro:bit file system.

  • UnsoundcodeUnsoundcode Posts: 1,530
    edited 2023-12-31 20:39

    The file is written to flash and if you use Thonny you may have to refresh the "file view" before you can see the file. Below is a better option for logging the same file.

    The list of lists is limited on the microbit because of the memory limitations and has more use on a desktop app. so the same thing written without using lists would be as simple as the following, a delay would probably be added in the range loop to sample the data at predetermined intervals.

    from microbit import *
    import random
    
    f = open('my.csv','w')
    
    my_header = str('Value 1' + ',' + 'Value 2' + ',' + 'Value 3' + '\n')
    
    f.write(my_header)
    
    for i in range(0,2000):
        val_1 = random.randint(0, 10)
        val_2 = random.randint(0, 10)
        val_3 = random.randint(0, 10)
        values= str(val_1) + ',' + str(val_2) + ',' + str(val_3) + '\n'
        f.write(values)
    
    f.close()
    

    Here is a screen shot on my PC

    EDIT: JonnyMac answered the question

  • If you look at the context menu in my screen shot you see an option to "Download to" which then lists the user folder, thats where you need to send your csv file.

  • I have not switched over to Thony yet, still using Python microbit.org. I checked the micro:bit file system, and it is not there. Not sure, but for the beginners like myself, that are not using Thony, should be a foolproof way of doing this in python microbit.org ide.

    Ray

  • JonnyMacJonnyMac Posts: 8,894
    edited 2023-12-31 22:18

    Does the environment you're using give you access to a REPL? If yes, you can look and see that the file is on the micro:bit.

    I tried that online editor -- complete garbage; it could see my v2 micro:bit but refused to connect. Switch to Thonny; you'll be more productive.

  • I created a VM for Linux Mint, ran Thonny there, and used it to look at the file with the REPL.

  • I just switched over to Thonny on my Win 10 PC. Now I am trying to figure out how to use Thonny microbit. I ran this csv example code, and it is showing up in the flash file system. I still cannot find the method for viewing the IO stuff. I opened up a shell, but that did not do it. Yes, it looks like Thonny will be more productive as soon as I figure out how to use it correctly.

    Ray

  • you need to use the microFS tool ufs
    ufs get my_file.csv will copy the file over to the current directorty on your PC
    the mu-editor also has a files option to make this a little more point and click

Sign In or Register to comment.