2

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()
Chris A
  • 23
  • 1
  • 5

1 Answers1

1

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))
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20
  • Does this work if a field _includes_ the separator, which is certainly possible and valid? – DisappointedByUnaccountableMod Jul 19 '21 at 21:58
  • This reads line by line, and gets rid of the new line syntax. This ensures data on every row, and the comma is delimiter. You can modify if you need to. But this should work on every properly formatted CSV file. – eatmeimadanish Jul 20 '21 at 04:23
  • “Properly formetted” CSV files can include comma and \n in fields (i.e. they can spread across multiple lines). So no, your code can’t handle generic CSV. – DisappointedByUnaccountableMod Jul 20 '21 at 07:06
  • eatmeimadanish Thanks for this. This has given me a list of lists. My next step is to learn how to find the max value for every nth entry in every list where i =<1. I think I can give that a try. – Chris A Jul 20 '21 at 07:36
  • balmy, I had already been making sure that my data didn't contain `\n` or `,` except for as delimiters so I'm happy with this solution. – Chris A Jul 20 '21 at 07:40
  • Theres always an unproductive "well actually" person just waiting to shout a useless one off scenario – eatmeimadanish Feb 16 '22 at 19:57
  • what if you need to split on multiple delimiters at the same time (e.g.: , ; /), how would you do it in MicroPython? `re` library seems to be very limited there. – InfiniteLoop Jul 01 '23 at 12:59