{"id":126915,"date":"2022-08-02T11:16:12","date_gmt":"2022-08-02T11:16:12","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=533759"},"modified":"2022-08-02T11:16:12","modified_gmt":"2022-08-02T11:16:12","slug":"how-to-convert-multiple-text-files-to-a-single-csv-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/08\/02\/how-to-convert-multiple-text-files-to-a-single-csv-in-python\/","title":{"rendered":"How to Convert Multiple Text Files to a Single 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;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;}\">\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<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>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" 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\/uploads\/2022\/08\/image-1-300x150.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-1-768x384.png 768w\" sizes=\"auto, (max-width: 832px) 100vw, 832px\" \/><figcaption><em><strong>Example<\/strong>: merge those files<\/em><\/figcaption><\/figure>\n<\/div>\n<p>Here&#8217;s the simple 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 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>\n<p>The resulting output CSV file shows that all text files have been merged:<\/p>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" 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\/uploads\/2022\/08\/image-2-300x170.png 300w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><\/figure>\n<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>\n<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>\n<p>The resulting CSV is neatly separated with comma characters:<\/p>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" 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\/uploads\/2022\/08\/image-3-300x182.png 300w\" sizes=\"auto, (max-width: 671px) 100vw, 671px\" \/><\/figure>\n<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>\n<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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) You can merge multiple text files to a single CSV file in Python by using the glob.glob(&#8216;.\/*.txt&#8217;) expression to filter out all path names of text files in a given folder. Then iterate over all those path names and use the open() function to read the file contents and write append [&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-126915","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\/126915","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=126915"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126915\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}