• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Reading numbers from external text file

Scheduled Pinned Locked Moved Developers' Forum
18 Posts 2 Posters 7.7k 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.
  • P Offline
    Pixero
    last edited by 30 Mar 2019, 17:35

    Can someone shed some light on how to read from an external text file and finding the relevant data?
    I have a txt file that originally is a json file and has some stuff that is not needed to read so how do I find the parts I want to read?

    Can you tell the script to read from certain lines?
    How do you cope with lines like these?

          "x"; -0.6789975732998361,
          "y"; 0.0008515437495198585
    

    If I know that for example line 66 will always be this: "x": (and some float) -0.6789975732998361, how do I skip the:

    "x";
    

    and the final comma?

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 30 Mar 2019, 18:55

      If it has a space after the : and you want the answer as a float number, then use:
      value = string.split(' ')[1].to_f - ignores any commas at end of string, it makes into a float
      OR if unsure, but there's always a : use:
      value = string.split(':')[1].lstrip.to_f

      To read ALL of the lines in a file use:
      lines = IO.readlines(file)

      If you definitely always want line 66
      string = lines[65]

      OR find it by its variable, there are many ways - e.g.

      string = ''
      lines.each{|line|
        if line.lstrip =~ /["]x["][;]/ ### strip off leading white-space & starts "x";
          string = line
          break ### take first match
        end
      }
      

      TIG

      1 Reply Last reply Reply Quote 0
      • P Offline
        Pixero
        last edited by 30 Mar 2019, 21:02

        Thanks. Ill try with this.

        1 Reply Last reply Reply Quote 0
        • P Offline
          Pixero
          last edited by 31 Mar 2019, 15:13

          Next question.
          This is the code I'm experimenting with:

          
          @settings = []
          
          def load_file(file_path, status)
            @filepath = UI.openpanel("Open File", "c;/", "TXT|*.txt||")
            @settings = File.readlines(@filepath)
            return 0   # Success
          end
          
          load_file(@filepath, true)
          
          puts @settings[2].split(' ')[1].to_f
          
          

          After loading the file I managed to get the number as a float but after running this I also get a:

          
          3.4919929866069115 #This is a number I want to read. So this is fine.
          "Nil result (no result returned or run failed)" #Why do I get this?
          
          
          1 Reply Last reply Reply Quote 0
          • T Offline
            TIG Moderator
            last edited by 31 Mar 2019, 17:59

            You don't need to return anything from the method.
            So delete the unneeded line.
            return 0 # Success

            You haven't posted all of your code, so it could be something elsewhere ?

            A simpler version would be...

            @settings = []
            def load_file()
              filepath = UI.openpanel("Open File", "c;/", "TXT|*.txt||")
              if filepath ### allows a user to Cancel
                @settings = File.readlines(filepath)
              end
            end
            load_file()
            puts @settings[2].split(' ')[1].to_f
            ### to see what @settings looks like use;
            p @settings
            ### This will show spacers, \", \n etc
            
            

            TIG

            1 Reply Last reply Reply Quote 0
            • P Offline
              Pixero
              last edited by 31 Mar 2019, 18:17

              Thanks, but I still get the Nil result even after removing the "Return 0".
              I even get it when running your code.

              For now I'm just experimenting with this part so there is no other code.

              The line I'm testing with looks like this:

              
                  "x"; 3.4919929866069115,
              
              
              1 Reply Last reply Reply Quote 0
              • T Offline
                TIG Moderator
                last edited by 31 Mar 2019, 18:20

                Did you use the whole code with p @settings at the end ?
                What do the first few lines of its output look like ?
                Also adding: puts @settings[2].split(' ')
                and puts @settings[2].split(' ')[1] to see what's up...

                TIG

                1 Reply Last reply Reply Quote 0
                • P Offline
                  Pixero
                  last edited by 31 Mar 2019, 18:23

                  
                  ["{\n", "  \"principalPoint\"; {\n", "    \"x\"; 3.4919929866069115,\n", "    \"y\"; -0.017301744356540385\n", "  },\n", "  \"viewTransform\"; {\n", "    \"rows\"; [\n", "      [\n", "        -0.9075910551963806,\n", "        0.4198243379864999,\n", "        -0.005099192261283809,\n", "        -31.940051338855746\n", "      ],\n", "      [\n", "        0.00419330408856825,\n", "        -0.003080616899800699,\n", "        -0.9999864629085424,\n", "        -0.10391287329462531\n", "      ],\n", "      [\n", "        -0.4198343634438959,\n", "        -0.9076001515170179,\n", "        0.0010354892297866703,\n", "        -17.250974969132418\n", "      ],\n", "      [\n", "        0,\n", "        0,\n", "        0,\n", "        1\n", "      ]\n", "    ]\n", "  },\n", "  \"cameraTransform\"; {\n", "    \"rows\"; [\n", "      [\n", "        -0.9075910551963807,\n", "        0.00419330408856825,\n", "        -0.41983436344389585,\n", "        -36.23062125433451\n", "      ],\n", "      [\n", "        0.41982433798650004,\n", "        -0.0030806168998006997,\n", "        -0.9076001515170179,\n", "        -2.2480967029645083\n", "      ],\n", "      [\n", "        -0.005099192261283809,\n", "        -0.9999864629085424,\n", "        0.0010354892297866703,\n", "        -0.24891673044480064\n", "      ],\n", "      [\n", "        0,\n", "        0,\n", "        0,\n", "        1\n", "      ]\n", "    ]\n", "  },\n", "  \"horizontalFieldOfView\"; 0.9813963064459168,\n", "  \"verticalFieldOfView\"; 0.6616350540688956,\n", "  \"vanishingPoints\"; [\n", "    {\n", "      \"x\"; -0.5541295463355106,\n", "      \"y\"; 0.0013923823242965234\n", "    },\n", "    {\n", "      \"x\"; 4.357757655415969,\n", "      \"y\"; -0.02365461394665286\n", "    },\n", "    {\n", "      \"x\"; 12.708844850226638,\n", "      \"y\"; 1807.4703594352652\n", "    }\n", "  ],\n", "  \"vanishingPointAxes\"; [\n", "    \"xPositive\",\n", "    \"yPositive\",\n", "    \"zPositive\"\n", "  ],\n", "  \"relativeFocalLength\"; 1.8716593429474995,\n", "  \"imageWidth\"; 5000,\n", "  \"imageHeight\"; 3214\n", "}"]
                  "[\"{\\n\", \"  \\\"principalPoint\\\"; {\\n\", \"    \\\"x\\\"; 3.4919929866069115,\\n\", \"    \\\"y\\\"; -0.017301744356540385\\n\", \"  },\\n\", \"  \\\"viewTransform\\\"; {\\n\", \"    \\\"rows\\\"; [\\n\", \"      [\\n\", \"        -0.9075910551963806,\\n\", \"        0.4198243379864999,\\n\", \"        -0.005099192261283809,\\n\", \"        -31.940051338855746\\n\", \"      ],\\n\", \"      [\\n\", \"        0.00419330408856825,\\n\", \"        -0.003080616899800699,\\n\", \"        -0.9999864629085424,\\n\", \"        -0.10391287329462531\\n\", \"      ],\\n\", \"      [\\n\", \"        -0.4198343634438959,\\n\", \"        -0.9076001515170179,\\n\", \"        0.0010354892297866703,\\n\", \"        -17.250974969132418\\n\", \"      ],\\n\", \"      [\\n\", \"        0,\\n\", \"        0,\\n\", \"        0,\\n\", \"        1\\n\", \"      ]\\n\", \"    ]\\n\", \"  },\\n\", \"  \\\"cameraTransform\\\"; {\\n\", \"    \\\"rows\\\"; [\\n\", \"      [\\n\", \"        -0.9075910551963807,\\n\", \"        0.00419330408856825,\\n\", \"        -0.41983436344389585,\\n\", \"        -36.23062125433451\\n\", \"      ],\\n\", \"      [\\n\", \"        0.41982433798650004,\\n\", \"        -0.0030806168998006997,\\n\", \"        -0.9076001515170179,\\n\", \"        -2.2480967029645083\\n\", \"      ],\\n\", \"      [\\n\", \"        -0.005099192261283809,\\n\", \"        -0.9999864629085424,\\n\", \"        0.0010354892297866703,\\n\", \"        -0.24891673044480064\\n\", \"      ],\\n\", \"      [\\n\", \"        0,\\n\", \"        0,\\n\", \"        0,\\n\", \"        1\\n\", \"      ]\\n\", \"    ]\\n\", \"  },\\n\", \"  \\\"horizontalFieldOfView\\\"; 0.9813963064459168,\\n\", \"  \\\"verticalFieldOfView\\\"; 0.6616350540688956,\\n\", \"  \\\"vanishingPoints\\\"; [\\n\", \"    {\\n\", \"      \\\"x\\\"; -0.5541295463355106,\\n\", \"      \\\"y\\\"; 0.0013923823242965234\\n\", \"    },\\n\", \"    {\\n\", \"      \\\"x\\\"; 4.357757655415969,\\n\", \"      \\\"y\\\"; -0.02365461394665286\\n\", \"    },\\n\", \"    {\\n\", \"      \\\"x\\\"; 12.708844850226638,\\n\", \"      \\\"y\\\"; 1807.4703594352652\\n\", \"    }\\n\", \"  ],\\n\", \"  \\\"vanishingPointAxes\\\"; [\\n\", \"    \\\"xPositive\\\",\\n\", \"    \\\"yPositive\\\",\\n\", \"    \\\"zPositive\\\"\\n\", \"  ],\\n\", \"  \\\"relativeFocalLength\\\"; 1.8716593429474995,\\n\", \"  \\\"imageWidth\\\"; 5000,\\n\", \"  \\\"imageHeight\\\"; 3214\\n\", \"}\"]"
                  
                  
                  
                  1 Reply Last reply Reply Quote 0
                  • P Offline
                    Pixero
                    last edited by 31 Mar 2019, 18:25

                    3.4919929866069115
                    "x";
                    3.4919929866069115,
                    "Nil result (no result returned or run failed)"
                    
                    1 Reply Last reply Reply Quote 0
                    • P Offline
                      Pixero
                      last edited by 31 Mar 2019, 18:26

                      3.4919929866069115
                      3.4919929866069115,
                      "Nil result (no result returned or run failed)"
                      
                      1 Reply Last reply Reply Quote 0
                      • P Offline
                        Pixero
                        last edited by 31 Mar 2019, 18:28

                        Here is the txt file I'm loading. (It's a json file from the beginning.)

                        test1.txt

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          TIG Moderator
                          last edited by 31 Mar 2019, 18:32

                          Are you running this in the Ruby Console, or in some other console extension ?
                          Try this without the ' def'

                          
                          filepath = UI.openpanel("Open File", "C;/", "TXT|*.txt||")
                          if filepath ### allows user to Cancel
                            settings = File.readlines(filepath)
                            puts settings[2].split(' ')[1].to_f
                          else
                            puts "Canceled!"
                          end
                          
                          

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • T Offline
                            TIG Moderator
                            last edited by 31 Mar 2019, 18:35

                            Our posts crossed.
                            Both my versions of the code work fine with your TXT file, with no error [in Ruby Console].
                            So do you use a console-app ?

                            TIG

                            1 Reply Last reply Reply Quote 0
                            • P Offline
                              Pixero
                              last edited by 31 Mar 2019, 18:40

                              Interesting.
                              Running your latest code in the console works but running it in SketchUp Ruby Code Editor by Alexander Schreyer gives the Nil result also.

                              I also tried your first version in the console and it worked without errors.
                              So is it safe to continue if it's just something in his Code editor app that causes the Nil result?

                              1 Reply Last reply Reply Quote 0
                              • T Offline
                                TIG Moderator
                                last edited by 31 Mar 2019, 18:43

                                Most of these console-apps are not perfect...
                                I only ever use the Ruby Console.
                                😉

                                TIG

                                1 Reply Last reply Reply Quote 0
                                • P Offline
                                  Pixero
                                  last edited by 31 Mar 2019, 18:45

                                  Thanks for all the help so far. I'm sure there will be more later on...
                                  If only the Ruby console was multi lines. It's hard to edit in the small window.

                                  1 Reply Last reply Reply Quote 0
                                  • T Offline
                                    TIG Moderator
                                    last edited by 31 Mar 2019, 19:38

                                    I always edit my code in a Notepad++/TextWrangler window [with Ruby text-highlighting], then I copy/paste it into the Ruby Console's bottom bar + enter, to run it.
                                    The Ruby Console accepts multi-line code if pasted, but you can only manually type in one line at a time!

                                    TIG

                                    1 Reply Last reply Reply Quote 0
                                    • P Offline
                                      Pixero
                                      last edited by 31 Mar 2019, 20:14

                                      I use Notepad++ as well but use the ruby code editor when testing out snippets without having to restart SketchUp.
                                      Now I’ll continue with the main work.

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

                                      Advertisement