pythonpython-2.7gisgrass

convert a set of coordinates from string to int


How do I convert my siteData which is a list of coordinates from string to int?

print(siteData)
    cat|x|y|z
    1|343741.396330097|1029255.04807763|0
    2|342270.660062496|1030198.57861216|0
    3|339743.874252208|1030705.47801259|0
    4|343031.709457817|1027263.37839108|0
    5|339453.578589758|1029954.65896125|0
    6|341376.669330771|1029549.5339487|0
    7|339224.904513178|1030438.74647616|0
    8|340285.317292692|1029431.45860403|0
    9|343135.800101739|1029804.27059289|0
    10|338982.428513304|1028857.42081259|0
    11|338405.612076696|1034287.30455498|0
    12|338080.457138754|1034337.35427931|0
    13|338944.90541069|1034519.90737124|0
    14|340963.209546547|1032528.77710582|0
    15|337891.948788692|1030626.74686363|0

Solution

  • Building upon Hozayfa's comment, you can do this as follows:

    lineWise = [line.strip() for line in siteData.split('\n')][1:]
    # This creates a list containing each row of siteData as a separate item and skips the header
    for row in lineWise:
        data = row.split('|')
        cat = int(data[0])
        x = int(float(data[1]))
        y = int(float(data[2]))
        z = int(float(data[3]))
    

    You can use variables cat, x, y and z inside the loop; they will be of type int.

    Edit: This is assuming that siteData looks like exactly this:

    cat|x|y|z
    1|343741.396330097|1029255.04807763|0
    2|342270.660062496|1030198.57861216|0
    3|339743.874252208|1030705.47801259|0
    4|343031.709457817|1027263.37839108|0
    5|339453.578589758|1029954.65896125|0
    6|341376.669330771|1029549.5339487|0
    7|339224.904513178|1030438.74647616|0
    8|340285.317292692|1029431.45860403|0
    9|343135.800101739|1029804.27059289|0
    10|338982.428513304|1028857.42081259|0
    11|338405.612076696|1034287.30455498|0
    12|338080.457138754|1034337.35427931|0
    13|338944.90541069|1034519.90737124|0
    14|340963.209546547|1032528.77710582|0
    15|337891.948788692|1030626.74686363|0