Select to view content in your preferred language

Create variable with Cursor and write it in a text file with Python

5275
15
Jump to solution
05-28-2015 05:37 AM
KONPETROV
Frequent Contributor

Hi, i am trying to create three variables from a table and write them in a text file with Python. These variables are values from the first row of three fields. I tried it with Search Cursor but i can get only the first variable, i don't know why. Next i am trying to do the same with a folder of polylines taking the first row of 2 fields using the same text i created above but unsuccesfully. Any suggestion would be great. Thanks a lot. This is my code:

import arcpy
import os
from arcpy import env

arcpy.env.workspace = "c:/Win/Sik"
input = "s3s2c10"
fields = ['X', 'Y', 'MOSAIC']

#NEW TXT FILE "MOON"
outFile = open("c:/Win/Sik/moon.text", "w")
with arcpy.da.SearchCursor(input, fields, 'OID = 1') as cursor:
    X = row[0]
    Y = row[1]
    IPSOS = row[2]

outFile.write('x is '+str(X)+'meters,'\n' Y is'+str(Y)+'and '\n' ipsos is'+str(IPSOS))
outfile.close()

import arcpy
import os
arcpy.env.workspace = "c:Win/Sik/True
outFile = open("c:/Win/Sik/moon.text", "w")
featureclasses = arcpy.ListFeatureClasses("Truelines", "Polyline", "")
for fc in featureclasses:
    TIME = row[0]
    DISTANCE = row[1] 
outFile.write( '\n''\n' 'TIME is '+str(TIME)+'meters,'\n' and DISTANCE is'+str(DISTANCE)+'meters'
outfile.close()
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Check that your input shapefile indeed has a field called 'OID' and not the more usual 'FID' field.

Compare:

>>> arcpy.env.workspace = r"C:\junk"
... input = "points"
... fields = ['heading','distance']
... with arcpy.da.SearchCursor(input,fields,"OID = 1") as cursor:
...     for row in cursor:
...         print row
...         
Runtime error 
Traceback (most recent call last):
  File "<string>", line 5, in <module>
RuntimeError: A column was specified that does not exist.
>>> arcpy.env.workspace = r"C:\junk"
... input = "points"
... fields = ['heading','distance']
... with arcpy.da.SearchCursor(input,fields,"FID = 1") as cursor:
...     for row in cursor:
...         print row
...         
(192, 533)

View solution in original post

15 Replies
LukeWebb
Frequent Contributor

For the 1st part (and the 2nd but well get to that later), you dont define 'row' anywhere, so I dont know where its getting the variable from!

To correct try adding the line 'for row in cursor:' as I have below:

import arcpy
import os
from arcpy import env

arcpy.env.workspace = "c:/Win/Sik"
input = "s3s2c10"
fields = ['X', 'Y', 'MOSAIC']

#NEW TXT FILE "MOON"
outFile = open("c:/Win/Sik/moon.text", "w")
with arcpy.da.SearchCursor(input, fields, 'OID = 1') as cursor:
    for row in cursor:
        X = row[0]
        Y = row[1]
        IPSOS = row[2]

outFile.write('x is '+str(X)+'meters,'\n' Y is'+str(Y)+'and '\n' ipsos is'+str(IPSOS))
outfile.close()
KONPETROV
Frequent Contributor

Luke i am getting a

Runtime error Traceback (most recent call last):   File "", line 12, in RuntimeError: A column was specified that does not exist.

but i cannot understand why

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Kon,

For your search cursor, try the following:

with arcpy.da.SearchCursor(input, fields, 'OID = 1') as cursor:
    for row in cursor:
        X = row[0] 
        Y = row[1] 
        IPSOS = row[2]

For the folder of polyline feature classes, if there are many feature classes you are trying to iterate through, you will want to use a wildcard.  You existing syntax will only find a feature class called 'Truelines'.  You may want to add try '*'.  This will iterate through all of the feature classes in the workspace.  Ex:

featureclasses = arcpy.ListFeatureClasses("*", "Polyline", "")  
for fc in featureclasses:
    with arcpy.da.SearchCursor(fc, fields) as cursor:
        for row in cursor:
            TIME = row[0]  
            DISTANCE = row[1]   
            outFile.write( '\n''\n' 'TIME is '+str(TIME)+'meters,'\n' and DISTANCE is'+str(DISTANCE)+'meters'  
outfile.close() 
KONPETROV
Frequent Contributor

Jake with the first block i am getting the same error as in Luke:

Runtime error Traceback (most recent call last):   File "", line 12, in RuntimeError: A column was specified that does not exist.

For the second block of code, "Trueline" is my wildcard. And i have  ap problem with the last line cause  i am getting an error

Parsing error SyntaxError: invalid syntax (line 12)

0 Kudos
DarrenWiens2
MVP Honored Contributor

Check that your input shapefile indeed has a field called 'OID' and not the more usual 'FID' field.

Compare:

>>> arcpy.env.workspace = r"C:\junk"
... input = "points"
... fields = ['heading','distance']
... with arcpy.da.SearchCursor(input,fields,"OID = 1") as cursor:
...     for row in cursor:
...         print row
...         
Runtime error 
Traceback (most recent call last):
  File "<string>", line 5, in <module>
RuntimeError: A column was specified that does not exist.
>>> arcpy.env.workspace = r"C:\junk"
... input = "points"
... fields = ['heading','distance']
... with arcpy.da.SearchCursor(input,fields,"FID = 1") as cursor:
...     for row in cursor:
...         print row
...         
(192, 533)
KONPETROV
Frequent Contributor

Οh, god. Yes you are right about that,  table has a Rowid starting from 1 and Polyline an FID starting from 0. But i am still getting an error Parsing error SyntaxError: unexpected character after line continuation character (line 17)

when i run it inside outfile to write it in my text.

0 Kudos
DarrenWiens2
MVP Honored Contributor

I don't believe you need all the quotes around the \n's:

outFile.write('x is ' + str(X) + 'meters,\n Y is' + str(Y) + 'and \n ipsos is' + str(IPSOS))

KONPETROV
Frequent Contributor

and of course you were right again! I have two last questions, can i have these two scripts together as a tool because these are two different workspaces.? How can i pass also the name of each shp ?

0 Kudos
OwenEarley
Frequent Contributor

To pass in the names of the shapefiles you can use arcpy.GetParameterAsText(). For an outline of how to do this see these pages:

Using parameters you could also pass in the different workspaces and switch between them when required.