07-11-2022, 09:29 PM
How to Convert .blf (CAN) to .csv in Python
<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"461066","slug":"default","valign":"top","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2>Problem Formulation</h2>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>How to convert <code>.blf</code> from a CAN bus to <code>.csv</code> in Python?</strong></p>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>What is BLF?</strong> The Binary Logging Format (<a rel="noreferrer noopener" href="https://python-can.readthedocs.io/en/master/_modules/can/io/blf.html" data-type="URL" data-id="https://python-can.readthedocs.io/en/master/_modules/can/io/blf.html" target="_blank">BLF</a>) is a proprietary<br />CAN log format from the automative company Vector Informatik GmbH.</p>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>What is CAN?</strong> The Controller Area Network (<a href="https://en.wikipedia.org/wiki/CAN_bus" data-type="URL" data-id="https://en.wikipedia.org/wiki/CAN_bus" target="_blank" rel="noreferrer noopener">CAN bus</a>) is a message-based protocol standard for microcontrollers in vehicles to communicate without a host computer.</p>
<h2>Method 1: Using BLF Reader and CSV Writer</h2>
<p class="has-global-color-8-background-color has-background">To convert the BLF file <code>'my_file.blf'</code> to the CSV file <code>'my_file.csv'</code>, you can first iterate over the bus messages using <code>can.BLFReader('my_file.csv')</code> and add the data to a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank">list of lists</a>. Then, you can use the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-convert-a-list-of-lists-to-a-csv-file-in-python/" data-type="post" data-id="7999" target="_blank">csv.writer()</a></code> approach to write the list of lists to a CSV file.</p>
<p>Here’s an example that improves upon <a rel="noreferrer noopener" href="https://stackoverflow.com/questions/49499903/how-do-i-convert-blf-data-from-can-to-csv-using-python" data-type="URL" data-id="https://stackoverflow.com/questions/49499903/how-do-i-convert-blf-data-from-can-to-csv-using-python" target="_blank">this</a> SO thread:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import can
import csv log = [] for msg in list(can.BLFReader("my_file.blf")): msg = str(msg) row = [msg[18:26], msg[38:40], msg[40:42], msg[46], msg[62], msg[67:90]] log.append(row) with open("my_file.csv", "w", newline='') as f: writer = csv.writer(f, delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL) writer.writerows(log)
</pre>
<p>A more sophisticated version of this code is provided in <a rel="noreferrer noopener" href="https://github.com/qqj1228/blf2csv/blob/master/blf2csv.py" data-type="URL" data-id="https://github.com/qqj1228/blf2csv/blob/master/blf2csv.py" target="_blank">this</a> Github repository. Here’s a screenshot of the code — notice the more advanced processing of a single message compared to our solution:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="756" height="1024" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-54-756x1024.png" alt="" class="wp-image-461123" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-54-756x1024.png 756w, https://blog.finxter.com/wp-content/uplo...22x300.png 222w, https://blog.finxter.com/wp-content/uplo...8x1040.png 768w, https://blog.finxter.com/wp-content/uplo...age-54.png 1104w" sizes="(max-width: 756px) 100vw, 756px" /><figcaption><a href="https://github.com/qqj1228/blf2csv/blob/master/blf2csv.py" data-type="URL" data-id="https://github.com/qqj1228/blf2csv/blob/master/blf2csv.py" target="_blank" rel="noreferrer noopener">source</a></figcaption></figure>
</div>
<h2>Method 2: Using the candas Library</h2>
<p>The <a rel="noreferrer noopener" href="https://gist.github.com/JulianWgs/2fa20fabfa87d709a0b6c394eba9266c" data-type="URL" data-id="https://gist.github.com/JulianWgs/2fa20fabfa87d709a0b6c394eba9266c" target="_blank"><code>candas</code></a> library provides utility functions to work with <code>.blf</code> files and the CAN bus. Among other things, it helps you with the conversion from BLF to CSV as outlined <a rel="noreferrer noopener" href="https://stackoverflow.com/questions/49499903/how-do-i-convert-blf-data-from-can-to-csv-using-python" data-type="URL" data-id="https://stackoverflow.com/questions/49499903/how-do-i-convert-blf-data-from-can-to-csv-using-python" target="_blank">here</a>.</p>
<p>This is the provided example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import candas as cd db = cd.load_dbc("dbc_folder") # This is the BLF file 'my_file.blf':
log = cd.from_file("my_file") # This prints a signal from the messages in the BLF:
print(log["AVGcellTemperature"])</pre>
<h2>Method 3: Using Custom Solution from python-can Library</h2>
<p>You can use your tailor-made solutions by combining the Readers and Writers provided in the <code><a href="https://python-can.readthedocs.io/en/master/listeners.html#csvwriter" data-type="URL" data-id="https://python-can.readthedocs.io/en/master/listeners.html#csvwriter" target="_blank" rel="noreferrer noopener">python-can</a></code> library.</p>
<p>It provides multiple utility functions such as:</p>
<ul>
<li><code>Listener</code></li>
<li><code>BufferedReader</code></li>
<li><code>RedirectReader</code></li>
<li><code>Logger</code></li>
<li><code>Printer</code></li>
<li><code>CSVWriter</code></li>
<li><code>SqliteWriter</code></li>
<li><code>ASC</code></li>
<li><code>Log</code></li>
<li><code>BLF</code></li>
</ul>
<p>Chances are you’ll find what you’re looking for when going over those functions!</p>
<h2>Related Video</h2>
<p>Still not satisfied? I found the following relevant video when searching for a solution to this problem. I think you’ll find some nice tricks in the video!</p>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Vector CANoe - Log File Conversion" width="780" height="439" src="https://www.youtube.com/embed/zWChGrRyMs4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
</div>
https://www.sickgaming.net/blog/2022/07/...in-python/
<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"461066","slug":"default","valign":"top","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2>Problem Formulation</h2>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>How to convert <code>.blf</code> from a CAN bus to <code>.csv</code> in Python?</strong></p>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>What is BLF?</strong> The Binary Logging Format (<a rel="noreferrer noopener" href="https://python-can.readthedocs.io/en/master/_modules/can/io/blf.html" data-type="URL" data-id="https://python-can.readthedocs.io/en/master/_modules/can/io/blf.html" target="_blank">BLF</a>) is a proprietary<br />CAN log format from the automative company Vector Informatik GmbH.</p>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>What is CAN?</strong> The Controller Area Network (<a href="https://en.wikipedia.org/wiki/CAN_bus" data-type="URL" data-id="https://en.wikipedia.org/wiki/CAN_bus" target="_blank" rel="noreferrer noopener">CAN bus</a>) is a message-based protocol standard for microcontrollers in vehicles to communicate without a host computer.</p>
<h2>Method 1: Using BLF Reader and CSV Writer</h2>
<p class="has-global-color-8-background-color has-background">To convert the BLF file <code>'my_file.blf'</code> to the CSV file <code>'my_file.csv'</code>, you can first iterate over the bus messages using <code>can.BLFReader('my_file.csv')</code> and add the data to a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank">list of lists</a>. Then, you can use the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-convert-a-list-of-lists-to-a-csv-file-in-python/" data-type="post" data-id="7999" target="_blank">csv.writer()</a></code> approach to write the list of lists to a CSV file.</p>
<p>Here’s an example that improves upon <a rel="noreferrer noopener" href="https://stackoverflow.com/questions/49499903/how-do-i-convert-blf-data-from-can-to-csv-using-python" data-type="URL" data-id="https://stackoverflow.com/questions/49499903/how-do-i-convert-blf-data-from-can-to-csv-using-python" target="_blank">this</a> SO thread:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import can
import csv log = [] for msg in list(can.BLFReader("my_file.blf")): msg = str(msg) row = [msg[18:26], msg[38:40], msg[40:42], msg[46], msg[62], msg[67:90]] log.append(row) with open("my_file.csv", "w", newline='') as f: writer = csv.writer(f, delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL) writer.writerows(log)
</pre>
<p>A more sophisticated version of this code is provided in <a rel="noreferrer noopener" href="https://github.com/qqj1228/blf2csv/blob/master/blf2csv.py" data-type="URL" data-id="https://github.com/qqj1228/blf2csv/blob/master/blf2csv.py" target="_blank">this</a> Github repository. Here’s a screenshot of the code — notice the more advanced processing of a single message compared to our solution:</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="756" height="1024" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-54-756x1024.png" alt="" class="wp-image-461123" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-54-756x1024.png 756w, https://blog.finxter.com/wp-content/uplo...22x300.png 222w, https://blog.finxter.com/wp-content/uplo...8x1040.png 768w, https://blog.finxter.com/wp-content/uplo...age-54.png 1104w" sizes="(max-width: 756px) 100vw, 756px" /><figcaption><a href="https://github.com/qqj1228/blf2csv/blob/master/blf2csv.py" data-type="URL" data-id="https://github.com/qqj1228/blf2csv/blob/master/blf2csv.py" target="_blank" rel="noreferrer noopener">source</a></figcaption></figure>
</div>
<h2>Method 2: Using the candas Library</h2>
<p>The <a rel="noreferrer noopener" href="https://gist.github.com/JulianWgs/2fa20fabfa87d709a0b6c394eba9266c" data-type="URL" data-id="https://gist.github.com/JulianWgs/2fa20fabfa87d709a0b6c394eba9266c" target="_blank"><code>candas</code></a> library provides utility functions to work with <code>.blf</code> files and the CAN bus. Among other things, it helps you with the conversion from BLF to CSV as outlined <a rel="noreferrer noopener" href="https://stackoverflow.com/questions/49499903/how-do-i-convert-blf-data-from-can-to-csv-using-python" data-type="URL" data-id="https://stackoverflow.com/questions/49499903/how-do-i-convert-blf-data-from-can-to-csv-using-python" target="_blank">here</a>.</p>
<p>This is the provided example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import candas as cd db = cd.load_dbc("dbc_folder") # This is the BLF file 'my_file.blf':
log = cd.from_file("my_file") # This prints a signal from the messages in the BLF:
print(log["AVGcellTemperature"])</pre>
<h2>Method 3: Using Custom Solution from python-can Library</h2>
<p>You can use your tailor-made solutions by combining the Readers and Writers provided in the <code><a href="https://python-can.readthedocs.io/en/master/listeners.html#csvwriter" data-type="URL" data-id="https://python-can.readthedocs.io/en/master/listeners.html#csvwriter" target="_blank" rel="noreferrer noopener">python-can</a></code> library.</p>
<p>It provides multiple utility functions such as:</p>
<ul>
<li><code>Listener</code></li>
<li><code>BufferedReader</code></li>
<li><code>RedirectReader</code></li>
<li><code>Logger</code></li>
<li><code>Printer</code></li>
<li><code>CSVWriter</code></li>
<li><code>SqliteWriter</code></li>
<li><code>ASC</code></li>
<li><code>Log</code></li>
<li><code>BLF</code></li>
</ul>
<p>Chances are you’ll find what you’re looking for when going over those functions!</p>
<h2>Related Video</h2>
<p>Still not satisfied? I found the following relevant video when searching for a solution to this problem. I think you’ll find some nice tricks in the video!</p>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Vector CANoe - Log File Conversion" width="780" height="439" src="https://www.youtube.com/embed/zWChGrRyMs4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
</div>
https://www.sickgaming.net/blog/2022/07/...in-python/