2

I used the following code to convert .shp file to .wkt.

The code was successful but where can I find the .wkt file (the print row ouput)?

Is it possible to add a path for the wkt files to be stored in?

import arcpy
InFile = r"C:\TEST.shp"
for row in arcpy.da.SearchCursor(InFile,["SHAPE@WKT"]):
    print row[0]
2

There are several approaches. I would use basic python file input/output. Something like below should work:

shp = r"C:\TEST.shp"
wkt = r"C:\TEST.wkt" # or TEST.txt
with open(wkt, "w") as f:
    for row in arcpy.da.SearchCursor(shp, ["SHAPE@WKT"]):
        f.write(row[0])
        f.write("\n") # insert new line

However, depending on what you plan to do with the WKT strings, you will need to format it properly. For example, see here.

0

Well, a simple way is to store it in python within a list:

import arcpy
InFile = r"C:\TEST.shp"

tobefilled = []   # creates an empty list which is filled by the cursor
for row in arcpy.da.SearchCursor(InFile,["SHAPE@WKT"]):
    print row[0]
    tobefilled.append(row[0])

print tobefilled

Or you create an empty file like this

text = open("output.txt", "w") # w stands for 'write' access

for row in arcpy.da.SearchCursor(InFile,["SHAPE@WKT"]):
    text.write(row[0]\n)

text.close()

Didn't test the code, but something like this should do.

  • Thanks, I run the first script and it said "Succeeded" but where can I find the "print tobefilled". This what happened with the code in question, it said succeeded but I cannot find the print outcomes. I thought it might be a ".wkt" file stored in the .shp file folder but it did not. I think I am missing something! – Dan Wood Aug 3 '17 at 16:15
  • I don't know where you execute your code. When you execute it in the Python window ArcMap (ArcGIS for Desktop 10.2) then it's directly printed in the python window. When I use my Editor (here: pycharm) then it's written to the Pyhon console. But Python doesn't create a new file unless you advice him to (see second example). So the first example is just written to the python interface. No wkt is created and no shape-file is changed. The print command gives you just a feedback on the screen! – GisKiste Aug 3 '17 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.