简体   繁体   中英

How do I read and analyse csv files in micropython?

I'm setting up a Raspberry Pi Pico to log temperature, humidity etc. to a csv file and to show data on a small OLED screen. Every hour it will log a new line of data delimited by commas to the file.

I want to be able to show the maximum and minimum recorded values as well as the most recent but I'm having real trouble parsing the file as anything other than text using micropython.

Micropython has no csv module, I can use split to separate the values by delimiter (,) but I don't know how to arrange that into rows using the (\\n) characters that are present in the file and I don't know how I would easily query that for min and max values in specific columns. I'd really appreciate your input. Apologies for any naiveté, I'm very new to coding.

edit: below is one of my many failed attempts to get the data to be interpreted as an array that can be interrogated. The non-functional ".line.split("\\n")" was an attempt to delimit the line breaks as they show up in the output following the "split(",")".

file = open('data45713.csv', 'r')
dataset = file.read().split(",").line.split("\n")
print (dataset)
file.close()

You can just open the file, and split on the delimiter ,

csvdata = []
delim = ','
with open('<File.csv>','r') as file:
    for line in file:
        csvdata.append(line.rstrip('\n').rstrip('\r').split(delim))

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