DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Ruby (http://www.daniweb.com/forums/forum73.html)
-   -   Help Handling CSV Columns (http://www.daniweb.com/forums/thread197510.html)

fightoplankton Jun 14th, 2009 8:36 pm
Help Handling CSV Columns
 
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!


All times are GMT -4. The time now is 5:18 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC