This is my first question on StackOverflow, so suggestions on how to make the question clearer are always welcome.
I have a CSV of point data structured as shown below:
OBJECTID,CART_ID,SHAPE
1,ABC,"(1.2, -4.5)"
2,ABD,"(3.8, 9.1)"
Using the petl module in python 3.5, I am trying to convert the SHAPE string into two separate columns of float objects. Using what I understood from the petl documentation, I should be able to do it in three steps.
- Open the CSV:
a = petl.fromcsv('file.csv')
- Convert SHAPE from a string to a tuple:
b = petl.convert(a, 'SHAPE', tuple)
- Split the tuple into two columns using .unpack():
c = petl.unpack(b, 'SHAPE', ['LAT', 'LON']
I believed this would result in a CSV file that looks like this:
OBJECTID,CART_ID,LAT,LON
1,ABC,1.2,-4.5
2,ABD,3.8,9.1
Instead, .convert() produces:
OBJECTID,CART_ID,SHAPE
1,ABC,('(', '1', '.', '2', ',', ' ', '-', '4', '.', '5', ')')
Any help on a) what .convert() is doing, or b) how to restructure the CSV would be appreciated.
Thank you.
Full code:
import petl
a = petl.fromcsv('file.csv')
petl.look(a)
b = petl.convert(a, 'SHAPE', tuple)
petl.look(b)
c = petl.unpack(b, 'SHAPE', ['LAT', 'LON']
petl.look(c)