Python CSV on VEX code

How would you create and access a .csv file on VEX code?

The first thing I would try is to import the csv module: https://docs.python.org/3/library/csv.html

If that module is not accessible from VEXcode python, then you’ll ned to parse/generate the files yourself. The CSV spec has more edge cases than you might think (which the csv module will take care of) but if you just want to read/write some numeric for sensor datalogging or whatever, you probably won’t have to worry about most of those.

# write some CSV data
with open("myfile.csv", "w") as f:
    f.write(f"{data1},{data2},{data3},{data4}")

# read some CSV data
data = []
with open("myfile.csv", "r") as f:
    for line in f.readlines():
        data.append(line.split(","))

More on file I/O in python here.

4 Likes

f-strings are not supported. This is an embedded Python VM and does not have all features that would be available on a PC.

open is only supported in the VM we released with VEXcode 2.0.7
file needs to be closed or data may not be flushed to the SD card.

# write some CSV data
with open("myfile.csv", "w") as f:
    f.write("{},{},{},{}".format(data1, data2, data3, data4 ))
    f.close()

# read some CSV data
data = []
with open("myfile.csv", "r") as f:
    for line in f.readlines():
        data.append(line.split(","))
4 Likes