Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Read and Convert a Binary File to CSV in Python?

#1
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="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;544306&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;}">
<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/...in-python/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016