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

4622
15
Jump to solution
05-28-2015 05:37 AM
KONPETROV
Occasional Contributor III

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
15 Replies
KONPETROV
Occasional Contributor III

Thank you Owen. It's just that i haven't used again two different workspaces inside a script.

Except from that for the iteration of shapefiles this is the code:

import arcpy  
import os
from arcpy import env
arcpy.env.workspace = "c:Win/Sik/Routes"  
outFile = open("c:/Win/Sik/moon.text", "w")  
featureclasses = arcpy.ListFeatureClasses("RT", "Polyline", "")
for fc in featureclasses:
    fields = ['DISTANCE', 'DURATION']
    with arcpy.da.SearchCursor(fc, fields, "FID = 0") as cursor:  
        for row in cursor:  
            TIME = row[0]    
            DISTANCE = row[1]     
outFile.write('TIME is ' + str(TIME) + 'meters,\n Y is' + str(DISTANCE) + 'and \n ipsos is' + str(DISTANCE))    
outfile.close()

but i get back that error

Runtime error

Traceback (most recent call last):

  File "<string>", line 7, in <module>

TypeError: 'NoneType' object is not iterable

although i have defined the type of shapefiles

0 Kudos
KONPETROV
Occasional Contributor III

What does is it mean, "is not iterable"? How can that be?

0 Kudos
BillDaigle
Occasional Contributor III

I think the arcpy.ListFeatureClasses command is returning None (which is not iterable) because the workspace is not getting set correctly in line 4.  Try updating line 4 to:

arcpy.env.workspace = "c:/Win/Sik/Routes"

0 Kudos
KONPETROV
Occasional Contributor III

i did it already Daigle, but still remains. I also applied this

import arcpy  
... import os
... from arcpy import env
... arcpy.env.workspace = "c:/Win/Sik/Routes"  
... outFile = open("c:/Win/Sik/moon.text", "w")  
... featureclasses = arcpy.ListFeatureClasses("RT", "Polyline")
... for fc in featureclasses:
...     fields = ['DISTANCE', 'DURATION']
...     with arcpy.da.SearchCursor(fc, fields, "FID = 0") as cursor:  
...         for row in cursor:
...             outFile.write('distance is ' + str(row[0]) + 'meters,\n duration is' + str(row[1])
... outFile.close()

but either it returns nothing or overwrites the previous results from text file leaving an empty file

0 Kudos
KONPETROV
Occasional Contributor III

I think i found it! The wildcard(*) for God. Now can i put these to scripts to be together as one or the second will overwrite the first?

mm.png.

0 Kudos
curtvprice
MVP Esteemed Contributor

Two issues with OID = 1 worth noting I think:

1. You can get the name o the object ID field (which can vary by data source and sometimes by workflow) you can do this:

OIDField  = arcpy.Describe(lyr).OIDFieldName

2. The first row can be object ID zero or one, depending on data source. If what you want the first row, I'd do this instead:

row = arcpy.da.SearchCursor(input, fields).next()
print row