topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday December 12, 2024, 10:53 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: In Python 2.7. how do I Read in Specific Text From a File and Assign to Variable  (Read 2178 times)

CodeTRUCKER

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,085
    • View Profile
    • Donate to Member
Hello,

How do I read in text as string instead of an integer?

Code: Python [Select]
  1. start = content.find("SimVars.0")
  2. latstart = content.find("Latitude=")
  3. latend = content.find("Latitude=",latstart+1)
  4. longstart = content.find("Longitude=",start)
  5. longend = content.find(",",longstart)

These lines are supposed to read in data as text, but all the values I get are these numbers (respectively)...

5628
5639
-1
5664
8795

The raw data from text file...

[SimVars.0]
Latitude=N21° 20' 47.36"
Longitude=W157° 27' 23.20"

What is wrong? 

Thanks.
Calvin
« Last Edit: June 02, 2022, 11:19 PM by CodeTRUCKER »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,190
    • View Profile
    • Donate to Member
I'm assuming you read the content into a string and that's what the content variable is as that's not specified.

Find returns the location of the first occurence of the string. You then have to use that to read the actual string. The -1 means it was not found.

https://www.geeksfor.../python-string-find/

CodeTRUCKER

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,085
    • View Profile
    • Donate to Member
Thanks w808. With your help light is beginning to break through, albeit somewhat fuzzy. I'm reading/re-reading the link you offered and the collateral information. I'm going to try and wrestle with this for a while and see if I can teach myself with the references. I may be back if I get stuck or lost.