2

I am running the following python code:

import arcpy
import polyline
import csv
import re

fc = "E:\\GoogleString\\CadastreCoordinates\\version1\\coordinates.gdb\\cadastre_wc_1"
csvfile = "E:\\GoogleString\\CadastreCoordinates\\version1\\WC\\output.csv"
cursor = arcpy.SearchCursor(fc)
coord_list = []
ltlong = []
final_strings=[]

for row in cursor:
    co = row.getValue("coordinates")
    sg = row.getValue("sg21key")
    testa = re.sub('(,[^,]*),', r'\1 ', co)
    testb = re.sub('[()]', '', testa)
    test2 = testb.split(' ')
    coord_list.append(test2)
    coords = [map(float,a.split(',')) for a in test2]
    ltlong.append([sg,coords])

for i in ltlong:
    string=polyline.encode(i[1],5)
    final_strings.append([i[0],string])

with open(csvfile, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    writer.writerows(final_strings)

On my sample fc I got the output that I wanted and my code runs successfully but when I run my whole fc it tells me that I can't convert string to float. But it is the same data just a sample. Please help don't know what to do anymore.

ERROR:

Traceback (most recent call last):
  File "C:\Handri\Python\google_str3.py", line 20, in <module>
    coords = [map(float,a.split(',')) for a in test2]
ValueError: could not convert string to float: 
DavidG
  • 24,279
  • 14
  • 89
  • 82
Handri
  • 21
  • 2
  • 1
    wrap that line in a `try` block and when it raises an error `print` the line (`a`) that raised it, Then you can debug it. I suspect a newline character (`'\n'`) – Ma0 Jul 12 '17 at 11:12
  • @pstatix First `strip()` then `split()`. `strip()` does not work on `list`s. – Ma0 Jul 12 '17 at 11:24
  • you could try opening your input data file in Notepad++ and then doing `view/show symbol/show all characters` - checkout things like end of line characters `\n` or `\r\n` - these have given me similar problems in the past. – jacanterbury Jul 12 '17 at 11:25
  • @pstatix `strip()` works on strings, not lists. The correct order would be `a.rstrip().split(',')`. By the way, why not strip from both sides: `a.strip().split(',')` – Sam Chats Jul 12 '17 at 11:25
  • Thanks everyone. It was my first time using a try block, and it indicated why it keeps on failing. Learned something new today! – Handri Jul 12 '17 at 11:36

0 Answers0