{"id":759,"date":"2010-10-15T13:59:52","date_gmt":"2010-10-15T17:59:52","guid":{"rendered":"http:\/\/mossiso.com\/?p=759"},"modified":"2010-10-15T13:59:52","modified_gmt":"2010-10-15T17:59:52","slug":"convert-csv-file-to-directories","status":"publish","type":"post","link":"https:\/\/mossiso.com\/2010\/10\/15\/convert-csv-file-to-directories\/","title":{"rendered":"Convert CSV file to directories"},"content":{"rendered":"<p>A fellow co-worker had a need to turn a CSV file into a series of directories and files where the columns of the CSV file were the directories, and the contents of the cells for each row were made into a text file and placed in the appropriate folder corresponding to the column. For example given the CSV file with the columns (name,email,number,date), and several rows like this:<\/p>\n<p>john,j@j.com,89,tuesday<\/p>\n<p>bertha,b@j.com,2716,monday<\/p>\n<p>dillan,d@j.com,289,saturday<\/p>\n<p>xavier,x@j.com,839,wednesday<\/p>\n<p>The script would create four numbered folders (1, 2, 3, 4), and put a file in each folder corresponding with the row number and column.<\/p>\n<figure id=\"attachment_764\" aria-describedby=\"caption-attachment-764\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/mossiso.com\/wp-content\/uploads\/2010\/10\/Screen-shot-2010-10-15-at-Oct-15-1.51.54-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-764\" title=\"Columns into folders, cells into text files\" src=\"http:\/\/mossiso.com\/wp-content\/uploads\/2010\/10\/Screen-shot-2010-10-15-at-Oct-15-1.51.54-PM.png\" alt=\"\" width=\"600\" height=\"106\" srcset=\"https:\/\/mossiso.com\/wp-content\/uploads\/2010\/10\/Screen-shot-2010-10-15-at-Oct-15-1.51.54-PM.png 600w, https:\/\/mossiso.com\/wp-content\/uploads\/2010\/10\/Screen-shot-2010-10-15-at-Oct-15-1.51.54-PM-300x53.png 300w, https:\/\/mossiso.com\/wp-content\/uploads\/2010\/10\/Screen-shot-2010-10-15-at-Oct-15-1.51.54-PM-250x44.png 250w, https:\/\/mossiso.com\/wp-content\/uploads\/2010\/10\/Screen-shot-2010-10-15-at-Oct-15-1.51.54-PM-150x26.png 150w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-764\" class=\"wp-caption-text\">Columns into folders, cells into text files<\/figcaption><\/figure>\n<p>So, within folder number one would be four text files named 1-1.txt, 2-1.txt, 3-1.txt, 4-1.txt. Text file 1-1.txt would contain the content &#8220;john&#8221;. File 3-1.txt would contain the text &#8220;dillan&#8221;. Within folder number 3 would be four text files named 1-3.txt, 2-3.txt, 3-3.txt, and 4-3.txt.<\/p>\n<p>Well, anyhow, I hope that explanation makes sense. Here&#8217;s the code:<\/p>\n<p>[code lang=&#8221;perl&#8221;]<br \/>\n#!\/usr\/bin\/perl<br \/>\nuse strict;<br \/>\nuse warnings;<br \/>\nuse Text::CSV::Encoded;<\/p>\n<p>my $csv = Text::CSV::Encoded-&gt;new();<\/p>\n<p># file to be used given on the command line as an argument to the script<br \/>\nopen (CSV, &#8220;&lt;:encoding(UTF-8)&#8221;, $ARGV[0]) or die $!;<\/p>\n<p>my $rownum = 1;<br \/>\nwhile () {<br \/>\nif ($csv-&gt;parse($_)) {<br \/>\n# Put the rows into an array<br \/>\nmy @columns = $csv-&gt;fields();<\/p>\n<p># Make the directories, as many as there are columns<br \/>\n# Get the number of columns (which is the number of elements in the<br \/>\n# array).<br \/>\nmy $size = scalar(@columns);<br \/>\nmy $count = 1;<br \/>\n# Check if the directories are created. Only want to make them on the<br \/>\n# interration through the first row<br \/>\nif ( !-d $count ) {<br \/>\nwhile ($count &lt;= $size) {                 mkdir &#8220;$count&#8221; or die $!;                 $count++;             }         }         # Now make a file from each cell, putting the file in the directory         # corresponding to the column         my $colnum = 1;         for my $column (@columns) {             # Create a new file (or overwrite an existing one) in the             # appropriate column folder and name it row#-col#.txt             open FILE, &#8220;+&gt;$colnum\/row$rownum-col$colnum.txt&#8221; or die $!;<br \/>\n# This sets the correct encoding on writing to the file<br \/>\nbinmode FILE, &#8220;:utf8&#8221;;<br \/>\nprint FILE $column;<br \/>\nclose FILE;<br \/>\n$colnum++;<br \/>\n}<\/p>\n<p>} else {<br \/>\nmy $err = $csv-&gt;error_input;<br \/>\nprint &#8220;Failed to parse line: $err&#8221;;<br \/>\n}<\/p>\n<p>$rownum++;<br \/>\n}<br \/>\nclose CSV;<\/p>\n<p>[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A fellow co-worker had a need to turn a CSV file into a series of directories and files where the columns of the CSV file were the directories, and the contents of the cells for each row were made into a text file and placed in the appropriate folder corresponding to the column. For example &hellip; <a href=\"https:\/\/mossiso.com\/2010\/10\/15\/convert-csv-file-to-directories\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Convert CSV file to directories<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":760,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[3,167],"tags":[],"class_list":["post-759","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","category-technical"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/mossiso.com\/wp-content\/uploads\/2010\/10\/Screen-shot-2010-10-15-at-Oct-15-1.35.26-PM.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9wosP-cf","_links":{"self":[{"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/posts\/759"}],"collection":[{"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/comments?post=759"}],"version-history":[{"count":4,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/posts\/759\/revisions"}],"predecessor-version":[{"id":766,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/posts\/759\/revisions\/766"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/media\/760"}],"wp:attachment":[{"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/media?parent=759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/categories?post=759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mossiso.com\/wp-json\/wp\/v2\/tags?post=759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}