{"id":126369,"date":"2022-07-08T12:19:09","date_gmt":"2022-07-08T12:19:09","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=461066"},"modified":"2022-07-08T12:19:09","modified_gmt":"2022-07-08T12:19:09","slug":"how-to-convert-blf-can-to-csv-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/07\/08\/how-to-convert-blf-can-to-csv-in-python\/","title":{"rendered":"How to Convert .blf (CAN) to .csv in Python"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;461066&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2>Problem Formulation<\/h2>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" 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>\n<p><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" 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>\n<p><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" 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>\n<h2>Method 1: Using BLF Reader and CSV Writer<\/h2>\n<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>\n<p>Here&#8217;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>\n<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\nimport 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)\n<\/pre>\n<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&#8217;s a screenshot of the code &#8212; notice the more advanced processing of a single message compared to our solution:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" 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\/uploads\/2022\/07\/image-54-222x300.png 222w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-54-768x1040.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/07\/image-54.png 1104w\" sizes=\"auto, (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>\n<\/div>\n<h2>Method 2: Using the candas Library<\/h2>\n<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>\n<p>This is the provided example:<\/p>\n<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':\nlog = cd.from_file(\"my_file\") # This prints a signal from the messages in the BLF:\nprint(log[\"AVGcellTemperature\"])<\/pre>\n<h2>Method 3: Using Custom Solution from python-can Library<\/h2>\n<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>\n<p>It provides multiple utility functions such as:<\/p>\n<ul>\n<li><code>Listener<\/code><\/li>\n<li><code>BufferedReader<\/code><\/li>\n<li><code>RedirectReader<\/code><\/li>\n<li><code>Logger<\/code><\/li>\n<li><code>Printer<\/code><\/li>\n<li><code>CSVWriter<\/code><\/li>\n<li><code>SqliteWriter<\/code><\/li>\n<li><code>ASC<\/code><\/li>\n<li><code>Log<\/code><\/li>\n<li><code>BLF<\/code><\/li>\n<\/ul>\n<p>Chances are you&#8217;ll find what you&#8217;re looking for when going over those functions!<\/p>\n<h2>Related Video<\/h2>\n<p>Still not satisfied? I found the following relevant video when searching for a solution to this problem. I think you&#8217;ll find some nice tricks in the video!<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<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>\n<\/div>\n<\/figure>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation How to convert .blf from a CAN bus to .csv in Python? What is BLF? The Binary Logging Format (BLF) is a proprietaryCAN log format from the automative company Vector Informatik GmbH. What is CAN? The Controller Area Network (CAN bus) is a message-based protocol standard for microcontrollers in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-126369","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=126369"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126369\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}