943,902 Members | Top Members by Rank

Ad:
  • Ruby Discussion Thread
  • Unsolved
  • Views: 2681
  • Ruby RSS
Jun 14th, 2009
0

Help Handling CSV Columns

Expand Post »
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.

Ruby Syntax (Toggle Plain Text)
  1. require 'rubygems'
  2. require 'roo'
  3. require 'fileutils'
  4. require 'rio'
  5.  
  6. FileUtils.mkdir_p "/Users/pshapiro/Desktop/Excel/xls"
  7. FileUtils.mkdir_p "/Users/pshapiro/Desktop/Excel/tmp"
  8. FileUtils.mkdir_p "/Users/pshapiro/Desktop/Excel/csv"
  9.  
  10. @filesxls = Dir["/Users/pshapiro/Desktop/Excel/*.xls"]
  11. for file in @filesxls
  12. FileUtils.move(file,"/Users/pshapiro/Desktop/Excel/xls")
  13. end
  14.  
  15. @filesxls = Dir["/Users/pshapiro/Desktop/Excel/xls/*.xls"]
  16. @filetmp = Dir["/Users/pshapiro/Desktop/Excel/xls/*.xls_tmp"]
  17.  
  18. for file in @filesxls
  19. convert = Excel.new(file)
  20. convert.default_sheet = convert.sheets[0]
  21. convert.to_csv(file+"_tmp")
  22. end
  23.  
  24. @filestmp = Dir["/Users/pshapiro/Desktop/Excel/xls/*.xls_tmp"]
  25.  
  26. for file in @filestmp
  27. FileUtils.move(file,"/Users/pshapiro/Desktop/Excel/tmp")
  28. end
  29.  
  30. dir = "/Users/pshapiro/Desktop/Excel/tmp/"
  31. files = Dir.entries(dir)
  32. files.each do |f|
  33. next if f == "." or f == ".."
  34. oldFile = dir + "/" + f
  35. newFile = dir + "/" + File.basename(f, '.*')
  36. File.rename(oldFile, newFile)
  37. end
  38.  
  39. files = Dir.entries(dir)
  40. files.each do |f|
  41. next if f == "." or f == ".."
  42. oldFile = dir + "/" + f
  43. newFile = dir + "/" + f + ".csv"
  44. File.rename(oldFile, newFile)
  45. end
  46.  
  47. @filescsv = Dir["/Users/pshapiro/Desktop/Excel/tmp/*.csv"]
  48.  
  49. for file in @filescsv
  50. FileUtils.move(file,"/Users/pshapiro/Desktop/Excel/csv")
  51. end
  52.  
  53. FileUtils.rm_rf("/Users/pshapiro/Desktop/Excel/tmp")
  54.  
  55. @filescsv = Dir["/Users/pshapiro/Desktop/Excel/csv/*.csv"]
  56.  
  57. for file in @filescsv
  58. 5.times {
  59. text=""
  60. File.open(file,"r"){|f|f.gets;text=f.read}
  61. File.open(file,"w+"){|f| f.write(text)}
  62. }
  63. end
  64.  
  65. rio("/Users/pshapiro/Desktop/Excel/csv/Attention.csv").csv.skipcolumns(1..2) > rio('/Users/pshapiro/Desktop/Excel/csv/Attention_Fixed.csv').csv(',')

Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fightoplankton is offline Offline
1 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Ruby Forum Timeline: command line argument -- ruby
Next Thread in Ruby Forum Timeline: watir and selectlist





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC