UnicodeDecodeError DataFileReader


I try to read a jls file to plot the data with matplotlib.pyplot. For that, I use the DataFileReader of file datafile.py.
My code is the following:

from joulescope import datafile
f = open("test_read.jls", "r")
r = datafile.DataFileReader(f)

I have tried on several jls files but I always get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "$HOME/.local/share/virtualenvs/joulescope_env/lib/python3.8/site-packages/joulescope/datafile.py", line 508, in __init__
    header = self._fh.read(HEADER_SIZE)
  File "/usr/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd3 in position 0: invalid continuation byte

Can anyone help me?

Hi @arno and welcome to the Joulescope forum!

A few things:

  1. joulescope.datafile contains the lower-level file format access. You likely want to use joulescope.data_recorder.DataReader. docs | code.
  2. If you are opening the file manually, you need to open it in binary mode with options ‘rb’, not just ‘r’.

So, your code could look something like:

from joulescope.data_recorder import DataReader
r = DataReader().open('test_read.jls')

A few notes:

  1. JLS files can easily contain more data than Matplotlib can handle. You may need to fetch and plot the statistics reductions. See joulescope.data_recorder.DataReader.statistics_get.
  2. The joulescope package can already plot the JLS samples for you. The JLS file does need to be short, and longer than 1 second of JLS data starts to overwhelm Matplotlib. Assuming you have your python/Scripts directory in your path, you can just type joulescope recording --plot test_read.jls. You can find the code here.
  3. We have a new JLS v2 format that is not backwards compatible with v1. The Joulescope UI can display them, but does not yet record to this format. If you are capturing data with scripts, you can use it. See this example. You can then read it with pyjls.Reader.
1 Like

Thank you very much for your answer. This is exactly what I needed. I finally made the plots with joulescope.data_recorder.DataReader.get_reduction so I could choose the axes I wanted and customize the style.

1 Like