DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

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

CodeTRUCKER

  • Bush Flying... where I'd rather be.
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,086
    • Participated in NANY Challenge Once
    • Military Service Badge - US Navy
Hello,

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

Code: ( python)
start = content.find("SimVars.0")
latstart = content.find("Latitude=")
latend = content.find("Latitude=",latstart+1)
longstart = content.find("Longitude=",start)
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
DISCLAIMER - I have made and make many threads for questions and even project posts across the Internet. Unfortunately, many are in limbo because I cannot remember where they are at. If anyone knows of an open thread or an unanswered inquiry, please PM me. Thank you.
   
Notice: Unless stated otherwise, I receive no compensation for anything I post here.
« Last Edit: June 03, 2022, 04:19 AM by CodeTRUCKER »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 11,205
    • Participated in NANY Challenge Fourteen Times
    • Coding Snacks
    • Writer
    • Hardware Hacker
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.geeksforgeeks.org/python-string-find/

CodeTRUCKER

  • Bush Flying... where I'd rather be.
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,086
    • Participated in NANY Challenge Once
    • Military Service Badge - US Navy
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.
DISCLAIMER - I have made and make many threads for questions and even project posts across the Internet. Unfortunately, many are in limbo because I cannot remember where they are at. If anyone knows of an open thread or an unanswered inquiry, please PM me. Thank you.
   
Notice: Unless stated otherwise, I receive no compensation for anything I post here.

Original address: https://www.donationcoder.com/forum/index.php?topic=52405.0