title: Lat/long python type conversion error unable to convert to float (2nd attempt)
body: <p>Right, I made a hash of asking this question when I was fried at the back end of yesterday so I'm doing it properly now.</p>
  
  <p>The block below converts some data into lat/long info and then there's an if statement where I can grab certain parts of a large JSON file which there's a section of below.</p>
  
  <p>What I want to do it take the forklift data, convert it's position (which is x and y meters from a known lat/long into an actual lat/long using Pythagoras) and then use the if statement to only print certain parts of the JSON (name, time, converted position).  The JSON consists of people &amp; machine info and I want to separate them.</p>
  
  <pre><code>"id": "b4994c877c9c",
  "name": "forklift_0001",  &lt;---forklift data used in IF statement
  "areaId": "Tracking001",
  "areaName": "hall_1",
  "color": "#FF0000",
  "coordinateSystemId": "CoordSys001",
  "coordinateSystemName": null,
  "covarianceMatrix": [
      0.47,
      0.06,
      0.06,
      0.61
  ],
  "position": [
      33.86,    &lt;---position data converted from known lat/long, X then Y.
      33.07,
      2.15
  ],
  "positionAccuracy": 0.36,
  "positionTS": 1489363199493,
  "smoothedPosition": [
      33.96,
      33.13,
      2.15
  ],
  "zones": [
      {
          "id": "Zone001",
          "name": "Halli1"
  </code></pre>
  
  <p>The problem I have seems to be that the first print statement is a float and to be able to pass the result into the if statement it needs to be a string.</p>
  
  <p>Here's the code I have</p>
  
  <pre><code>for f in file_list:
  print('Input file: ' + f) # Replace with desired operations
  
  with open(f, 'r') as f:
  
      distros = json.load(f)
      output_file = 'forklift_0001_parse' + str(output_nr) + '.csv' #output file name may be changed
  
      with open(output_file, 'w') as text_file:
          for distro in distros:      
              position = distro['position']
              R = 6378.1 #Radius of the Earth
              brng = 1.57 #Bearing is 90 degrees converted to radians.
              d = math.sqrt((position[0]*position[0] + position[1]*position[1]) + 0.00303) #Pythagoras formula to work distance from ref lat/long point
  
              lat1 = math.radians(60.477719)#Reference lat point 
              lon1 = math.radians(26.941589)#Reference long point 
  
              lat2 = math.asin(math.sin(lat1)*math.cos(d/R) + 
              math.cos(lat1)*math.sin(d/R)*math.cos(brng))
  
              lon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),
              math.cos(d/R)-math.sin(lat1)*math.sin(lat2))
  
              lat2 = math.degrees(lat2)
              lon2 = math.degrees(lon2)
              print((lat2, lon2), file=text_file) 
  
              if distro['name'].startswith(('Trukki_0001')): #choose desired parameters to be parsed
                  print (str(distro['name']))
  
                  print(distro['name'] + ',' + str(distro['positionTS']) + ',' + str(distro['position']) + ',' + str(distro['lat2']), file=text_file)
                  #replace desired roots between square brackets
  
      print('Output written to file: ' + output_file)
      output_nr = output_nr + 1
  </code></pre>
  
  <p>The error this gives is </p>
  
  <pre><code>Traceback (most recent call last):
  File "position_parse_3.py", line 45, in &lt;module&gt;
  print(distro['name'] + ',' + str(distro['positionTS']) + ',' + 
  str(distro['position']) + ',' + str(distro['lat2']), file=text_file)
  KeyError: 'lat2'
  </code></pre>
  
  <p>I am completely stuck and have tried a few ways to solve it. I've tried to convert them from floats earlier in the script that first prints statement but that did not work (can't convert them implicitly) and I am also concerned I'll lose the accuracy of the result. </p>
  
  <p>If someone could help me pass the result of the lat/long calculation into the IF statement, i would be eternally grateful.</p>