Search code examples
variablestextsaveassign

In Python 2.7. how do I Read in Specific Text From a File and Assign to Variable


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

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 I get are these numbers (respectively)...

5639
-1
5664
8795

The raw data from file...

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

What is wrong? Thanks.


Solution

  • Here's a way to do what I believe you're trying for in your question:

    content='''
    [SimVars.0]
    Latitude=N21° 20' 47.36"
    Longitude=W157° 27' 23.20"
    '''
    
    latKey = "Latitude="
    longKey = "Longitude="
    
    try:
        latstart = content.index(latKey) + len(latKey)
        latend = content.find('"', latstart) + 1
        longstart = content.find(longKey, latend) + len(longKey)
        longend = content.find('"', longstart) + 1
    except (ValueError):
        print("couldn't find latitude and longitude")
    
    print(f'latstart {latstart}')
    print(f'latend {latend}')
    print(f'longstart {longstart}')
    print(f'longend {longend}')
    
    lat = content[latstart:latend]
    long = content[longstart:longend]
    
    print()
    print(f'lat {lat}')
    print(f'long {long}')
    

    Output:

    latstart 22
    latend 37
    longstart 48
    longend 64
    
    lat N21° 20' 47.36"
    long W157° 27' 23.20"
    

    Comments on you question:

    The find() method of python's built-in str data type returns the position of the first argument if found, and -1 if not found. To get text, you will need to do something like lat = content[latstart:latend] after using find() to set latstart and latend.

    The index() method of str is an alternative to find() which in a success scenario does the same thing, but in a failure scenario will raise a ValueError exception (instead of returning -1). If you catch this exception using a try/catch block as in the code above, it saves you the trouble of checking the return values of individual calls to find() for -1, and takes some of the guesswork out of debugging your code.

    UPDATE:

    In response to OP's comment that print() is throwing an exception, given that you are using python version 2.7, let me simplify the code to see if we can get you something that works in your environment:

    from __future__ import print_function
    content='''
    [SimVars.0]
    Latitude=N21° 20' 47.36"
    Longitude=W157° 27' 23.20"
    '''
    
    latKey = "Latitude="
    longKey = "Longitude="
    
    latstart = content.index(latKey) + len(latKey)
    latend = content.find('"', latstart) + 1
    longstart = content.find(longKey, latend) + len(longKey)
    longend = content.find('"', longstart) + 1
    
    print('latstart ', latstart)
    print('latend ', latend)
    print('longstart ', longstart)
    print('longend ', longend)
    
    lat = content[latstart:latend]
    long = content[longstart:longend]
    
    print()
    print('lat ', lat)
    print('long ', long)
    

    Output:

    latstart  22
    latend  37
    longstart  48
    longend  64
    
    lat  N21° 20' 47.36"
    long  W157° 27' 23.20"