# Help Handling CSV Columns

**URL:** <https://www.ruby-forum.com/t/help-handling-csv-columns/170253>\
**Category:** Ruby\
**Created:** [June 15, 2009, 1:31am UTC](https://www.ruby-forum.com/t/help-handling-csv-columns/170253 "2009-06-15T01:31:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![azgar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@azgar](https://www.ruby-forum.com/u/azgar)\
**Post date:** [June 15, 2009, 1:31am UTC](https://www.ruby-forum.com/t/help-handling-csv-columns/170253/1 "2009-06-15T01:31:43Z")

</div>

I have been struggling for days on just the first part of this script.  
Please help! I need to add the ability to delete the last column of each  
csv, which vary in their lengths.

require ‘rubygems’  
require ‘roo’  
require ‘fileutils’  
require ‘rio’

FileUtils.mkdir\_p “/Users/pshapiro/Desktop/Excel/xls”  
FileUtils.mkdir\_p “/Users/pshapiro/Desktop/Excel/tmp”  
FileUtils.mkdir\_p “/Users/pshapiro/Desktop/Excel/csv”

@filesxls = Dir["/Users/pshapiro/Desktop/Excel/\*.xls"]  
for file in @filesxls  
FileUtils.move(file,"/Users/pshapiro/Desktop/Excel/xls")  
end

@filesxls = Dir["/Users/pshapiro/Desktop/Excel/xls/_.xls"]  
 @filetmp = Dir["/Users/pshapiro/Desktop/Excel/xls/_.xls\_tmp"]

for file in @filesxls  
convert = Excel.new(file)  
convert.default\_sheet = convert.sheets[0]  
convert.to\_csv(file+"\_tmp")  
end

@filestmp = Dir["/Users/pshapiro/Desktop/Excel/xls/\*.xls\_tmp"]

for file in @filestmp  
FileUtils.move(file,"/Users/pshapiro/Desktop/Excel/tmp")  
end

dir = “/Users/pshapiro/Desktop/Excel/tmp/”  
files = Dir.entries(dir)  
files.each do |f|  
next if f == “.” or f == “…”  
oldFile = dir + “/” + f  
newFile = dir + “/” + File.basename(f, ‘.\*’)  
File.rename(oldFile, newFile)  
end

files = Dir.entries(dir)  
files.each do |f|  
next if f == “.” or f == “…”  
oldFile = dir + “/” + f  
newFile = dir + “/” + f + “.csv”  
File.rename(oldFile, newFile)  
end

@filescsv = Dir["/Users/pshapiro/Desktop/Excel/tmp/\*.csv"]

for file in @filescsv  
FileUtils.move(file,"/Users/pshapiro/Desktop/Excel/csv")  
end

FileUtils.rm\_rf("/Users/pshapiro/Desktop/Excel/tmp")

@filescsv = Dir["/Users/pshapiro/Desktop/Excel/csv/\*.csv"]

for file in @filescsv  
5.times {  
text=""  
File.open(file,“r”){|f|f.gets;text=f.read}  
File.open(file,“w+”){|f| f.write(text)}  
}  
end

rio("/Users/pshapiro/Desktop/Excel/csv/Attention.csv").csv.skipcolumns(1…2)

> rio(’/Users/pshapiro/Desktop/Excel/csv/Attention\_Fixed.csv’).csv(’,’)

Thanks!

---

<div class="post-metadata">

**Author:** ![azgar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@azgar](https://www.ruby-forum.com/u/azgar)\
**Post date:** [June 15, 2009, 3:54am UTC](https://www.ruby-forum.com/t/help-handling-csv-columns/170253/2 "2009-06-15T03:54:19Z")

</div>

On Jun 14, 2009, at 6:31 PM, Paul Shapiro wrote:

> I have been struggling for days on just the first part of this script.  
> Please help! I need to add the ability to delete the last column of  
> each  
> csv, which vary in their lengths.

I’m not familiar with rio, but the process using most CSV parsers  
would be:

- Write a loop that reads and writes CSV (copies it effectively)
- Instead of writing the whole row, just write all the fields but the  
last one with something like: row[0…-2]

I hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![azgar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@azgar](https://www.ruby-forum.com/u/azgar)\
**Post date:** [June 15, 2009, 7:35am UTC](https://www.ruby-forum.com/t/help-handling-csv-columns/170253/3 "2009-06-15T07:35:50Z")

</div>

James G. wrote:

> On Jun 14, 2009, at 6:31 PM, Paul Shapiro wrote:
> 
> > I have been struggling for days on just the first part of this script.  
> > Please help! I need to add the ability to delete the last column of  
> > each  
> > csv, which vary in their lengths.
> 
> I’m not familiar with rio, but the process using most CSV parsers  
> would be:
> 
> - Write a loop that reads and writes CSV (copies it effectively)
> - Instead of writing the whole row, just write all the fields but the  
> last one with something like: row[0…-2]
> 
> I hope that helps.
> 
> James Edward G. II

THANK YOU SO MUCH!!!

require ‘rubygems’  
require ‘roo’  
require ‘csv’  
require ‘fileutils’  
require ‘rio’  
require ‘fastercsv’

FileUtils.mkdir\_p “/Users/pshapiro/Desktop/Excel/xls”  
FileUtils.mkdir\_p “/Users/pshapiro/Desktop/Excel/tmp”  
FileUtils.mkdir\_p “/Users/pshapiro/Desktop/Excel/csv”

@filesxls = Dir["/Users/pshapiro/Desktop/Excel/\*.xls"]  
for file in @filesxls  
FileUtils.move(file,"/Users/pshapiro/Desktop/Excel/xls")  
end

@filesxls = Dir["/Users/pshapiro/Desktop/Excel/xls/_.xls"]  
 @filetmp = Dir["/Users/pshapiro/Desktop/Excel/xls/_.xls\_tmp"]

for file in @filesxls  
convert = Excel.new(file)  
convert.default\_sheet = convert.sheets[0]  
convert.to\_csv(file+"\_tmp")  
end

@filestmp = Dir["/Users/pshapiro/Desktop/Excel/xls/\*.xls\_tmp"]

for file in @filestmp  
FileUtils.move(file,"/Users/pshapiro/Desktop/Excel/tmp")  
end

dir = “/Users/pshapiro/Desktop/Excel/tmp/”  
files = Dir.entries(dir)  
files.each do |f|  
next if f == “.” or f == “…”  
oldFile = dir + “/” + f  
newFile = dir + “/” + File.basename(f, ‘.\*’)  
File.rename(oldFile, newFile)  
end

files = Dir.entries(dir)  
files.each do |f|  
next if f == “.” or f == “…”  
oldFile = dir + “/” + f  
newFile = dir + “/” + f + “.csv”  
File.rename(oldFile, newFile)  
end

@filescsv = Dir["/Users/pshapiro/Desktop/Excel/tmp/\*.csv"]

for file in @filescsv  
FileUtils.move(file,"/Users/pshapiro/Desktop/Excel/csv")  
end

FileUtils.rm\_rf("/Users/pshapiro/Desktop/Excel/tmp")

@filescsv = Dir["/Users/pshapiro/Desktop/Excel/csv/\*.csv"]

for file in @filescsv  
5.times {  
text=""  
File.open(file,“r”){|f|f.gets;text=f.read}  
File.open(file,“w+”){|f| f.write(text)}  
}  
end

dir = “/Users/pshapiro/Desktop/Excel/csv/”  
files = Dir.entries(dir)  
files.each do |f|  
next if f == “.” or f == “…”  
oldFile = dir + “/” + f  
newFile = dir + “/” + File.basename(f, ‘.\*’) + “.tmp”  
File.rename(oldFile, newFile)  
end

@filescsv = Dir["/Users/pshapiro/Desktop/Excel/csv/\*.tmp"]

for file in @filescsv  
csv = FasterCSV.read(file, :headers =\> true)  
lastc = csv.headers.length-1  
puts lastc  
rio(file).csv.skipcolumns(1…2,lastc) \> rio(file+".csv").csv(’,’)  
end

@filescsv = Dir["/Users/pshapiro/Desktop/Excel/csv/\*.tmp"]

for file in @filescsv  
FileUtils.remove(file)  
end

dir = “/Users/pshapiro/Desktop/Excel/csv”  
files = Dir.entries(dir)  
files.each do |f|  
next if f == “.” or f == “…”  
oldFile = dir + “/” + f  
newFile = dir + “/” + File.basename(f, ‘.\*’)  
File.rename(oldFile, newFile)  
end

2.times {  
files = Dir.entries(dir)  
files.each do |f|  
next if f == “.” or f == “…”  
oldFile = dir + “/” + f  
newFile = dir + “/” + File.basename(f, ‘.\*’)  
File.rename(oldFile, newFile)  
end  
}

files = Dir.entries(dir)  
files.each do |f|  
next if f == “.” or f == “…”  
oldFile = dir + “/” + f  
newFile = dir + “/” + f + “.csv”  
File.rename(oldFile, newFile)  
end
