[Tut] How to Read and Convert a Binary File to CSV in Python? - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Read and Convert a Binary File to CSV in Python? (/thread-99785.html) |
[Tut] How to Read and Convert a Binary File to CSV in Python? - xSicKxBot - 08-09-2022 How to Read and Convert a Binary File to CSV in Python? <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"544306","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> <p class="has-global-color-8-background-color has-background">To read a binary file, use the <code>open('rb')</code> function within a context manager (<code>with</code> keyword) and read its content into a string variable using <code>f.readlines()</code>. You can then <a rel="noreferrer noopener" href="https://blog.finxter.com/python-convert-string-to-csv-file/" data-type="post" data-id="505474" target="_blank">convert the string to a CSV</a> using various approaches such as the <code><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/" data-type="post" data-id="503412" target="_blank" rel="noreferrer noopener">csv</a></code> module. </p> <p>Here’s an example to read the binary file <code>'my_file.man'</code> into your Python script:</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="">with open('my_file.man', 'rb') as f: content = f.readlines() print(content)</pre> <p>Per default, Python’s built-in <code><a href="https://blog.finxter.com/python-open-function/" data-type="post" data-id="24793">open()</a></code> function opens a text file. If you want to open a binary file, you need to add the <code>'b'</code> character to the optional <code>mode</code> string argument.</p> <ul> <li>To open a file for <strong><em>reading in binary format</em></strong>, use <code>mode='rb'</code>. </li> <li>To open a file for <strong><em>writing in binary format</em></strong>, use <code>mode='rb'</code>. </li> </ul> <p>Now that the content is in your Python script, you can convert it to a CSV using the various methods outlined in this article:</p> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Learn More</strong>: <a href="https://blog.finxter.com/python-convert-string-to-csv-file/" data-type="post" data-id="505474" target="_blank" rel="noreferrer noopener">Convert a String to CSV in Python</a></p> <p>After you’ve converted the data to the comma-separated values (CSV) format demanded by your application, you can write the string to a file using either the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-print/" data-type="post" data-id="20731" target="_blank">print()</a></code> function with file argument or the standard <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-one-liner-write-string-to-file/" data-type="post" data-id="10913" target="_blank">file.write()</a></code> approach.</p> </div> https://www.sickgaming.net/blog/2022/08/05/how-to-read-and-convert-a-binary-file-to-csv-in-python/ |