Reading numbers from external text file

Reading numbers from external text file

Postby Pixero » Sat Mar 30, 2019 6:35 pm

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?
Code: Select all
      "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:
Code: Select all
"x":
and the final comma?
0
User avatar
Pixero 
 

Re: Reading numbers from external text file

Postby TIG » Sat Mar 30, 2019 7:55 pm

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.
Code: Select all
string = ''
lines.each{|line|
  if line.lstrip =~ /["]x["][:]/ ### strip off leading white-space & starts "x":
    string = line
    break ### take first match
  end
}
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Reading numbers from external text file

Postby Pixero » Sat Mar 30, 2019 10:02 pm

Thanks. Ill try with this.
0
User avatar
Pixero 
 

Re: Reading numbers from external text file

Postby Pixero » Sun Mar 31, 2019 4:13 pm

Next question.
This is the code I'm experimenting with:
Code: Select all
@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:

Code: Select all
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?
0
User avatar
Pixero 
 

Re: Reading numbers from external text file

Postby TIG » Sun Mar 31, 2019 6:59 pm

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...
Code: Select all
@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
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Reading numbers from external text file

Postby Pixero » Sun Mar 31, 2019 7:17 pm

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:
Code: Select all
    "x": 3.4919929866069115,
0
User avatar
Pixero 
 

Re: Reading numbers from external text file

Postby TIG » Sun Mar 31, 2019 7:20 pm

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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Reading numbers from external text file

Postby Pixero » Sun Mar 31, 2019 7:23 pm

Code: Select all
["{\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\", \"}\"]"

0
User avatar
Pixero 
 

Re: Reading numbers from external text file

Postby Pixero » Sun Mar 31, 2019 7:25 pm

Code: Select all
3.4919929866069115
"x":
3.4919929866069115,
"Nil result (no result returned or run failed)"
0
User avatar
Pixero 
 

Re: Reading numbers from external text file

Postby Pixero » Sun Mar 31, 2019 7:26 pm

Code: Select all
3.4919929866069115
3.4919929866069115,
"Nil result (no result returned or run failed)"
0
User avatar
Pixero 
 

Re: Reading numbers from external text file

Postby Pixero » Sun Mar 31, 2019 7:28 pm

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

test1.txt
0
User avatar
Pixero 
 

Re: Reading numbers from external text file

Postby TIG » Sun Mar 31, 2019 7:32 pm

Are you running this in the Ruby Console, or in some other console extension ?
Try this without the 'def'
Code: Select all
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
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Reading numbers from external text file

Postby TIG » Sun Mar 31, 2019 7:35 pm

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 ?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Reading numbers from external text file

Postby Pixero » Sun Mar 31, 2019 7:40 pm

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?
0
User avatar
Pixero 
 

Re: Reading numbers from external text file

Postby TIG » Sun Mar 31, 2019 7:43 pm

Most of these console-apps are not perfect...
I only ever use the Ruby Console.
;)
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Reading numbers from external text file

Postby Pixero » Sun Mar 31, 2019 7:45 pm

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.
0
User avatar
Pixero 
 

Re: Reading numbers from external text file

Postby TIG » Sun Mar 31, 2019 8:38 pm

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!
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Reading numbers from external text file

Postby Pixero » Sun Mar 31, 2019 9:14 pm

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.
0
User avatar
Pixero 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 8 guests

Visit our sponsors: