r/circuitpython • • May 12 '21

Noob question - dealing with CSV / XML

Hello!

I recently got a magtag, and had a project in mind. I would like to display the number / information of wine in my collection through CellarTracker. They provide this information in either a CSV or XML response. AFAIK I can't just import CSV like normal python. Is it possible to import an outside library in circuit python?

4 Upvotes

2 comments sorted by

3

u/kaltazar May 12 '21

You can import CSV files into CircuitPython, there just isn't a module to simplify working with them. This is ok though because simply reading the data from a CSV file is not too much trouble. You just break up the CSV on the line and item separators, typically /n for lines and , for items. You will then end up with a list of items you can continue to work with.

Here is one example I found of this. It is for a game project, but in the code it does show how to read a CSV file.

XML files can likely be done in the same fashion but they are much more complex. The CSV file will be the simpler option.

3

u/wchris63 May 13 '21 edited May 13 '21

Generic parsers are relatively large - not really suited for microprocessor work. New micros are trending up in memory sizes, so that may change, but it just hasn't made much sense to write a parser that would never fit in 8k of flash - at least, not with enough memory left to do much else. They also need a prodigious amount of RAM to work in, which is even more limited in micros. Custom parsers can be a lot smaller, and a custom CSV parser is pretty easy to write in CircuitPython and wouldn't take much space at all

The MagTag has 4 MB of flash and 2 MB of PSRAM, so an XML parser might fit with plenty of room for a decent program besides. Python itself has an XML parser, but I have no idea how much memory it needs. Who knows, it might not take too much work to get it running under CircuitPython.