0

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

3 Answers3

0

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
Prateek Dewan
  • 1,587
  • 3
  • 16
  • 29
  • Prateeks, tried your suggestion and it returns Traceback (most recent call last): File "", line 3, in ValueError: invalid literal for int() with base 10: '' – grassgis1234 May 15 '20 at 19:37
  • Can you try adding a `print` statement inside the for loop and see what line is throwing this error? The answer is based on the assumption that your siteData variable is the exact string that you mentioned in the question. Please double-check that as well... – Prateek Dewan May 15 '20 at 19:39
0

I think the csv module is better suited for parsing such data:

import csv
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
"""
points = []
reader = csv.DictReader(siteData.strip().splitlines(), delimiter="|", quotechar='"')
for row in reader:
    point = {
        "id": int(row["cat"]), 
        "x": float(row["x"]),
        "y": float(row["y"]),
        "z": float(row["z"])
    }
    points.append(point)

for point in points:
    print(point)

Output:

{'id': 1, 'x': 343741.396330097, 'y': 1029255.04807763, 'z': 0.0}
{'id': 2, 'x': 342270.660062496, 'y': 1030198.57861216, 'z': 0.0}
{'id': 3, 'x': 339743.874252208, 'y': 1030705.47801259, 'z': 0.0}
{'id': 4, 'x': 343031.709457817, 'y': 1027263.37839108, 'z': 0.0}
{'id': 5, 'x': 339453.578589758, 'y': 1029954.65896125, 'z': 0.0}
{'id': 6, 'x': 341376.669330771, 'y': 1029549.5339487, 'z': 0.0}
{'id': 7, 'x': 339224.904513178, 'y': 1030438.74647616, 'z': 0.0}
{'id': 8, 'x': 340285.317292692, 'y': 1029431.45860403, 'z': 0.0}
{'id': 9, 'x': 343135.800101739, 'y': 1029804.27059289, 'z': 0.0}
{'id': 10, 'x': 338982.428513304, 'y': 1028857.42081259, 'z': 0.0}
{'id': 11, 'x': 338405.612076696, 'y': 1034287.30455498, 'z': 0.0}
{'id': 12, 'x': 338080.457138754, 'y': 1034337.35427931, 'z': 0.0}
{'id': 13, 'x': 338944.90541069, 'y': 1034519.90737124, 'z': 0.0}
{'id': 14, 'x': 340963.209546547, 'y': 1032528.77710582, 'z': 0.0}
{'id': 15, 'x': 337891.948788692, 'y': 1030626.74686363, 'z': 0.0}
Ionut Ticus
  • 2,683
  • 2
  • 17
  • 25
0

Using Pandas

import pandas as pd
from io import StringIO 

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"""

# wrap the siteData string data in StringIO function
# use pandas read_csv to create DataFrame
df = pd.read_csv(StringIO(siteData), sep ="|", dtype=float).astype(int)
print(df)

Output

    cat       x        y  z
0     1  343741  1029255  0
1     2  342270  1030198  0
2     3  339743  1030705  0
3     4  343031  1027263  0
4     5  339453  1029954  0
5     6  341376  1029549  0
6     7  339224  1030438  0
7     8  340285  1029431  0
8     9  343135  1029804  0
9    10  338982  1028857  0
10   11  338405  1034287  0
11   12  338080  1034337  0
12   13  338944  1034519  0
13   14  340963  1032528  0
14   15  337891  1030626  0
DarrylG
  • 16,732
  • 2
  • 17
  • 23