• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Import CSV to extract values

Scheduled Pinned Locked Moved Developers' Forum
7 Posts 4 Posters 1.6k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    kybasche
    last edited by 10 Jan 2012, 21:18

    I've searched the web for quite some time now, stumbling over the various explanations on how to import CSV files into arrays in Sketchup's Ruby.

    I think, if it's possible, I just need a dumbed down explanation on how to do it; my Ruby force is very very weak.

    Let's say I have the following as a CSV:

    1,2,3,4,5
    yes,no,2

    I would like to then create an array to hold each of the rows of values, such that I end up with:

    array_1 = [1, 2, 3, 4, 5]
    array_2 = ["yes", "no", 2]

    When I follow http://groups.google.com/group/sketchupruby/browse_thread/thread/d25a302dece3b21b/7c11cb4541613773?lnk=gst&q=foltz+csv#7c11cb4541613773
    using:

    csv_data = []
    f = File.open(path,'r')
    f.each_line{|line| csv_data.push line.split(",")}
    
    

    I end up with:
    csv_data = [["1.0", "2.0", "3.0", "4.0", "5.0\n"], ["yes", "no", "2.0\n"]]

    note the end of line indicators still being present (\n)

    Using the next line:

    headers = csv_data.shift.map {|i| i.to_s } 
    

    returns:
    headers = ["1.0", "2.0", "3.0", "4.0", "5.0\n"]
    where headers[0] = 1.0, headers[1] = 2.0, etc.
    That's pretty good.

    The last line:

    string_data = csv_data.map {|row| row.map {|cell| cell.to_s } } 
    

    returns:
    string_data = [["yes", "no", "2.0\n"]]
    where string_data[0] = ["yes", "no", "2.0\n"]
    Not great.

    using a similar line as for "headers":

    headers2 = string_data.shift.map {|i| i.to_s } 
    

    gets me to:
    headers2 = ["yes", "no", "2.0\n"]

    Better,
    but still with that pesky \n.

    Is there a better way to do this? And how do I avoid the \n?
    What about if there are more than 2 lines?

    I looked over Jim Foltz/Dan Rathbun's method, but I don't understand the code well enough to manipulate the parts that I want to use (it includes an IO schmealy that I'm not familiar with).

    Any hints would be greatly appreciated. Thanks much as always!

    Derek

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 10 Jan 2012, 23:12

      Try
      lines=IO.readlines(csvfilepath)

      >>> ["1,2,3,4,5\n", "yes,no,2\n"]

      Then use

      array=[]
      lines.each{|line|
        line.chomp!
        ### >>> "1,2,3,4,5"
        array << line.split(",")
        ### >>> ["1", "2", "3", "4", "5"]
      }
      

      Then to convert from 'strings' to say an integer use .to_i or a float .to_f on the 'columns' thus - let's use the second line (array[1]) as an example, you might want to set 'yes/no' to true/false booleans, and the 3rd column to a number if you can...

      if array[1][0].downcase=="yes"
        test0=true
      else
        test0=false
      end
      ### etc
      if array[1][2]=~/^[.0-9]/
        test2=array[1][2].to_f
      else ### leave it as a string ?
        test2=array[1][2]
      end
      
      

      How you deal with the data once you have it in an array is up to you 😮

      TIG

      1 Reply Last reply Reply Quote 0
      • K Offline
        kybasche
        last edited by 11 Jan 2012, 01:05

        Brilliant.

        Thanks!
        D

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 13 Jan 2012, 04:21

          see topic: [Code] reading a CSV file

          Ruby help questions belong in the Developers forum,
          where you can find the [Code Snippets] index.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • K Offline
            Ken007
            last edited by 23 Mar 2013, 15:02

            Hey!
            I have a problem that's been bothering me for quite a while now. Its like this that I have created a few 3D models in Sketchup 8 (the freely downloadable version) and now I want to geo locate them as per their GPS Co-ordinates (so i'm not looking to go through the usual 'add location button'. Now that thing is, I have written a script for sketchup to open the .skp files that contain the 3D models that I've created and I've linked it to another script that adds the GPS location as well as exports it to the desired location. The problem is that all of this happens through a GUI that the script generates. I want all this to occur directly through a database (for example MS Excel) such that the "name of the model", the "GPS co-ordinates/lat-longs" are taken as an input from the Excel database. The code I've written is as follows: (It is fully functional, though, I'm relatively new to sketchup so dont mind if it's a wee bit shabby)

            This is the code that opens the 3D model::

            prompts = ["Status"]
             defaults = [""]
             list = ["House|Garden|Palace|Dog house"]
             input = UI.inputbox prompts, defaults, list, "Choose a template" 
             
             if input[0].to_s.eql? "Garden" 
             result = Sketchup.open_file "E;\\GoogleEarth\\FINALFINAL\\NewFolder\\garden.skp"
             
             elsif input[0].to_s.eql? "House" 
             result = Sketchup.open_file "E;\\GoogleEarth\\FINALFINAL\\NewFolder\\house.skp"
             elsif input[0].to_s.eql? "Palace" 
             result = Sketchup.open_file "E;\\GoogleEarth\\FINALFINAL\\NewFolder\\palace.skp"
             elsif input[0].to_s.eql? "Dog house" 
             result = Sketchup.open_file "E;\\GoogleEarth\\FINALFINAL\\NewFolder\\dog_house.skp"
             
             end
             
             load 'C;\Documents and Settings\91014276\Desktop\untitled(3).rb'
            

            and this is 'untitled(3).rb' that actually geo locates the 3D model and exports it to the desired location::

            prompts = ["Latitude", "Longitude", "Location", "Destination Drive"]
             defaults = ["", "", "xyz", "E;\\Examples"]
             list = ["", "", ""]
             input = UI.inputbox prompts, defaults, list, "About the WEC"
             
            
            
            model = Sketchup.active_model
            
            # This is the bit that actually sets the geo-location.
              shadowinfo = model.shadow_info
              shadowinfo["City"] = "Unknown"
              shadowinfo["Country"] = "Unknown"
              shadowinfo["Latitude"] = input[0].to_f
              shadowinfo["Longitude"] = input[1].to_f
            
            georef = model.georeferenced?
            
            key = "GeoReference"
              model.set_attribute key, "GeoReferenceNorthAngle", 358.646700673226
              model.set_attribute key, "UsesGeoReferencing", true
              model.set_attribute key, "Longitude" , input[1].to_f
              model.set_attribute key, "Latitude" ,  input[0].to_f
              model.set_attribute key, "ModelTranslationX" ,  -12901579.2256146
              model.set_attribute key, "ModelTranslationY" , -214086056.635273
              model.set_attribute key, "ModelHereState", ""
              model.set_attribute key, "ModelHereZoom", 19
              model.set_attribute key, "ModelTranslationZ" , 0.0
              model.set_attribute key, "LocationSource" , "Google Earth"
              model.set_attribute key, "ZValueCentered" ,  0.0
              model.set_attribute key, "TimeStamp", 1296598526
              model.set_attribute key, "ModelHereState", ""
              model.set_attribute key, "ModelHereZoom", 19
            
            # Export model to kmz file for Google Earth
            	
              model.export "#{input[3]}\\ #{input[2]}.kmz", true
              
              result = UI.messagebox "Is that all or would you like to try another?", MB_YESNO
             if result == 6 # Yes
               load 'C;\Documents and Settings\91014276\Desktop\untitled(5).rb'
             
             elsif result == 7 # No
               UI.messagebox ("Alrighty then!")
               
               end
            
            

            In the above code 'input[0].to_f' and 'input[1].to_f' are the Latitude and Longitude co-ordinates where the model has to be placed.

            Im in need of some desperate help here so please...err..help!

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 23 Mar 2013, 16:41

              Ken... I've given you an example in the 'parallel thread' that you started.

              TIG

              1 Reply Last reply Reply Quote 0
              • K Offline
                Ken007
                last edited by 30 Mar 2013, 05:32

                @tig said:

                Ken... I've given you an example in the 'parallel thread' that you started.

                Yep, got that TIG. Thanks..and sorry for repeating the post.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement