Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Build a query string in Python

$
0
0
Hi, I am trying to write a small Python script to build a query string so ArcGIS can use it to select data by attribute.
In this script it first reads a text file (attachments) with 5 lot/plan details (each in separate line), then build a single line query string accordingly.

Here is my script to build the query string:
#===============
f_count = open("C:\GeoSpatialTraining\ArcGIS10\GISProgramming101\Exercises\Data\Local data\LotPlan_List.txt", "r")
#Count records number
numRec = len(f_count.readlines())
print "There are " + str(numRec) + " records in the file."

f = open("C:\GeoSpatialTraining\ArcGIS10\GISProgramming101\Exercises\Data\Local data\LotPlan_List.txt", "r")
sQuery = ""
i = 0

for rec in f:
#print rec
i = i + 1
if i < numRec:
sQuery = sQuery + "\"LOT_PLAN\" = '" + rec + "' OR "
else:
sQuery = sQuery + "\"LOT_PLAN\" = '" + rec + "'"
#print sQuery
f.close()
print sQuery

#======================

This is what I'd expect as a result:

"LOT_PLAN" = '68SP245201' OR "LOT_PLAN" = '7SP101558' OR "LOT_PLAN" = '8RP620660' OR "LOT_PLAN" = '8SP239672' OR "LOT_PLAN" = '9SP239672'

But it ends up like this:

"LOT_PLAN" = '68SP245201
' OR "LOT_PLAN" = '7SP101558
' OR "LOT_PLAN" = '8RP620660
' OR "LOT_PLAN" = '8SP239672
' OR "LOT_PLAN" = '9SP239672
'

Can anyone help please? Thanks.
Attached Files

Viewing all articles
Browse latest Browse all 2485

Trending Articles