csv nil check and update

Discussion in 'Ruby' started by Geoff, Mar 15, 2006.

  1. Geoff

    Geoff Guest

    Greetings!

    What I have is a .csv file (comma separated and quote delimited):

    "BegDoc","EndDoc","New"
    "Doc1BegDoc","Doc1EndDoc","Test1"
    "Doc2BegDoc","Doc2EndDoc",""
    "Doc3BegDoc","Doc3EndDoc","Test2"
    "Doc4BegDoc","Doc4EndDoc",""
    "Doc5BegDoc","Doc5EndDoc","New"

    I can read the lines of the file with this:

    require 'CSV'
    csvData = CSV.readlines("C:\\temp\\geoff\\filldown\\filldown.txt")

    Now what I want to do is check for blanks and when I find one I want to
    take the info from the entry directly above and fill down the column
    until the next blank. Using the above example, I want the following
    output:

    "BegDoc","EndDoc","New"
    "Doc1BegDoc","Doc1EndDoc","Test1"
    "Doc2BegDoc","Doc2EndDoc","Test1"
    "Doc3BegDoc","Doc3EndDoc","Test2"
    "Doc4BegDoc","Doc4EndDoc","Test2"
    "Doc5BegDoc","Doc5EndDoc","New"

    Any help is greatly appreciated!

    Thanks,

    Geoff
     
    Geoff, Mar 15, 2006
    #1
    1. Advertising

  2. On Mar 15, 2006, at 2:23 PM, Geoff wrote:

    > Now what I want to do is check for blanks and when I find one I
    > want to
    > take the info from the entry directly above and fill down the column
    > until the next blank. Using the above example, I want the following
    > output:
    >
    > "BegDoc","EndDoc","New"
    > "Doc1BegDoc","Doc1EndDoc","Test1"
    > "Doc2BegDoc","Doc2EndDoc","Test1"
    > "Doc3BegDoc","Doc3EndDoc","Test2"
    > "Doc4BegDoc","Doc4EndDoc","Test2"
    > "Doc5BegDoc","Doc5EndDoc","New"
    >
    > Any help is greatly appreciated!


    See if this gives you some ideas:

    Neo:~/Desktop$ ls
    csv_filldown.rb data.csv
    Neo:~/Desktop$ cat data.csv
    "BegDoc","EndDoc","New"
    "Doc1BegDoc","Doc1EndDoc","Test1"
    "Doc2BegDoc","Doc2EndDoc",""
    "Doc3BegDoc","Doc3EndDoc","Test2"
    "Doc4BegDoc","Doc4EndDoc",""
    "Doc5BegDoc","Doc5EndDoc","New"
    Neo:~/Desktop$ cat csv_filldown.rb
    #!/usr/local/bin/ruby -w

    require "csv"

    last = ""

    CSV.foreach(ARGV.shift) do |row|
    if row[-1].empty?
    row[-1] = last
    else
    last = row[-1]
    end

    p row
    end

    __END__
    Neo:~/Desktop$ ruby csv_filldown.rb data.csv
    ["BegDoc", "EndDoc", "New"]
    ["Doc1BegDoc", "Doc1EndDoc", "Test1"]
    ["Doc2BegDoc", "Doc2EndDoc", "Test1"]
    ["Doc3BegDoc", "Doc3EndDoc", "Test2"]
    ["Doc4BegDoc", "Doc4EndDoc", "Test2"]
    ["Doc5BegDoc", "Doc5EndDoc", "New"]

    James Edward Gray II
     
    James Edward Gray II, Mar 15, 2006
    #2
    1. Advertising

  3. Geoff

    Geoff Guest

    That does give me some ideas... thanks!

    Now:

    "Initialize" cannot convert nil to a string.

    Any ideas on that one?
     
    Geoff, Mar 15, 2006
    #3
  4. Geoff

    Geoff Guest

    Got it, nevermind!

    Thanks a ton for your help. :)
     
    Geoff, Mar 15, 2006
    #4
  5. Geoff

    ChrisH Guest

    You have an answer, but since I spent some time on it, here's mine! 9^)

    require 'csv'
    csvData = CSV.readlines("d:\\ruby\\dev\\filldown-csv\\filldown.txt")
    puts 'Before:'
    csvData.each {|l| p l}

    1.upto(csvData.size - 1){ |i|
    0.upto(csvData.size - 1){|j|
    csvData[j] ||= csvData[i-1][j]
    }
    }
    puts 'After:'
    csvData.each {|l| p l}


    For some reason my CSV wouldn't read the data when it has quotes around
    the values...

    cheers
    Chris
     
    ChrisH, Mar 15, 2006
    #5
  6. Geoff

    Geoff Guest

    Ok, obiously I'm doing something wrong again. I am new to both
    programming and to Ruby, so please excuse the low brow questions!

    I've now got this because I really want to take the result and output
    to a new file, but it does not work:

    require 'CSV'

    last = ""
    newFile = File.open("C:\\temp\\geoff\\filldown\\filldownNew.txt", "w+")
    CSV.foreach("C:\\temp\\geoff\\filldown\\filldown.txt") do |row|
    if row[-1].empty?
    row[-1] = last
    else
    last = row[-1]
    end
    newFile << (p row)
    end

    Ideas?

    Thanks!

    Geoff
     
    Geoff, Mar 15, 2006
    #6
  7. Geoff

    Geoff Guest

    Thanks, I appreciate it. For some reason the output is the same as the
    input when I try this though. Not sure why it does not work.
     
    Geoff, Mar 15, 2006
    #7
  8. On Mar 15, 2006, at 5:48 PM, Geoff wrote:

    > Ok, obiously I'm doing something wrong again. I am new to both
    > programming and to Ruby, so please excuse the low brow questions!
    >
    > I've now got this because I really want to take the result and output
    > to a new file, but it does not work:
    >
    > require 'CSV'
    >
    > last = ""
    > newFile = File.open("C:\\temp\\geoff\\filldown\\filldownNew.txt", "w
    > +")


    Change the above to:

    newFile = CSV.open(...)

    > CSV.foreach("C:\\temp\\geoff\\filldown\\filldown.txt") do |row|
    > if row[-1].empty?
    > row[-1] = last
    > else
    > last = row[-1]
    > end
    > newFile << (p row)


    And this to:

    newFile << row

    > end
    >
    > Ideas?


    Also, just FYI, the Ruby naming convention for variables is
    like_this, not likeThis.

    Hope that helps.

    James Edward Gray II
     
    James Edward Gray II, Mar 16, 2006
    #8
  9. Geoff

    ChrisH Guest

    Do you close the file?

    I'm pretty sure output is buffered and if the file is not closed
    properly it will not get flushed to disk.

    Cheers
     
    ChrisH, Mar 16, 2006
    #9
  10. Geoff

    ChrisH Guest

    Geoff wrote:
    > Thanks, I appreciate it. For some reason the output is the same as the
    > input when I try this though. Not sure why it does not work.


    It occurred to me this morning (what else am I going to think about on
    the bus 9^) that since your able to read the file with the
    double-quotes, you need to check for missing field using '.empty?'
    rather than '= nil'

    cheers
     
    ChrisH, Mar 16, 2006
    #10
  11. Geoff

    Geoff Guest

    Sweet... that's certainly an improvement, but as you mention I do need
    to retain the double quotes!
     
    Geoff, Mar 16, 2006
    #11
  12. Geoff

    Geoff Guest

    Also, another thing that this should do eventually is to do this for
    each column.
     
    Geoff, Mar 16, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candler

    puts nil generates "nil\n"

    Brian Candler, Nov 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    120
  2. John Carter
    Replies:
    64
    Views:
    676
    Klaus Stein
    May 19, 2005
  3. ako...

    a == nil or a.nil?

    ako..., Nov 22, 2005, in forum: Ruby
    Replies:
    6
    Views:
    153
    Douglas Livingstone
    Nov 23, 2005
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    273
    Christoffer Sawicki
    Sep 2, 2006
  5. Bigmac Turdsplash
    Replies:
    15
    Views:
    183
    Paul Smith
    Sep 8, 2009
Loading...

Share This Page