Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert Multiple Text Files to a Single CSV in Python?

#1
How to Convert Multiple Text Files to a Single 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;533759&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">You can merge multiple text files to a single CSV file in Python by using the <code>glob.glob('./*.txt')</code> expression to filter out all path names of text files in a given folder. Then iterate over all those path names and use the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" data-type="post" data-id="24793" target="_blank">open()</a></code> function to read the file contents and write append them to the CSV.</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="832" height="416" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-1.png" alt="" class="wp-image-533826" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-1.png 832w, https://blog.finxter.com/wp-content/uplo...00x150.png 300w, https://blog.finxter.com/wp-content/uplo...68x384.png 768w" sizes="(max-width: 832px) 100vw, 832px" /><figcaption><em><strong>Example</strong>: merge those files</em></figcaption></figure>
</div>
<p>Here’s the simple 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 glob with open('my_file.csv', 'a') as csv_file: for path in glob.glob('./*.txt'): with open(path) as txt_file: txt = txt_file.read() + '\n' csv_file.write(txt) </pre>
<p>The resulting output CSV file shows that all text files have been merged:</p>
<figure class="wp-block-image size-full"><img loading="lazy" width="567" height="321" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-2.png" alt="" class="wp-image-533838" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-2.png 567w, https://blog.finxter.com/wp-content/uplo...00x170.png 300w" sizes="(max-width: 567px) 100vw, 567px" /></figure>
<p>You can replace the separator (e.g., from single empty space to comma) by using the <code><a href="https://blog.finxter.com/python-string-replace-2/" data-type="post" data-id="26083" target="_blank" rel="noreferrer noopener">txt.replace(' ', ',')</a></code> function before writing it in the CSV:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="7" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import glob with open('my_file.csv', 'a') as csv_file: for path in glob.glob('./*.txt'): with open(path) as txt_file: txt = txt_file.read() + '\n' txt = txt.replace(' ', ',') csv_file.write(txt) </pre>
<p>The resulting CSV is neatly separated with comma characters:</p>
<figure class="wp-block-image size-full"><img loading="lazy" width="671" height="407" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-3.png" alt="" class="wp-image-533875" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-3.png 671w, https://blog.finxter.com/wp-content/uplo...00x182.png 300w" sizes="(max-width: 671px) 100vw, 671px" /></figure>
<p>In case you need some more advanced ways to convert the text files to the CSV, you may want to check out the Pandas <code><a href="https://blog.finxter.com/how-to-read-specific-columns-from-csv-file-in-python/" data-type="post" data-id="347248">read_csv()</a></code> function to read the CSV into a DataFrame.</p>
<p>As soon as you have it as a DataFrame, you can do advanced processing such as <a href="https://blog.finxter.com/pandas-dataframe-merge-method/" data-type="post" data-id="344000" target="_blank" rel="noreferrer noopener">merging</a>, <a href="https://blog.finxter.com/how-to-select-rows-from-a-dataframe-based-on-column-values/" data-type="post" data-id="220687" target="_blank" rel="noreferrer noopener">column selection</a>, <a href="https://blog.finxter.com/slicing-data-from-a-pandas-dataframe-using-loc-and-iloc/" data-type="post" data-id="230997">slicing</a>, etc.</p>
<p class="has-base-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>Related Tutorial</strong>: <a href="https://blog.finxter.com/read-a-csv-file-to-a-pandas-dataframe/" data-type="post" data-id="440655">How to Read a CSV to a DataFrame?</a></p>
</div>


https://www.sickgaming.net/blog/2022/08/...in-python/
Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016