reformat your code so that indentations are readily read
import os file = r'C:\GeoSpatialTraining\ArcGIS10\GISProgramming101\Exercises\Data\Local data\LotPlan_List.txt' f_count = open(file, "r") #Count records number numRec = len(f_count.readlines()) print("There are {0} records in the file.".format(numRec)) f = open(file, "r") field = 'LOT_PLAN' lots_list = [] for rec in f: rec = rec.rstrip('\n') lots_list.append(rec) sQuery = '''"{0}" in ('{1}')'''.format(field, "', '".join(lots_list)) f.close() print(sQuery)
The indentations seem fine in my script, it's only when I paste the code in the message box here that all indentations were removed.
The indentations seem fine in my script, it's only when I paste the code in the message box here that all indentations were removed
queries = [] qry = '"LOT_PLAN" = \'{0}\'' for rec in f: queries.append(qry.format(rec)) f.close() sQuery = " OR ".join(queries) print sQuery
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 = "" queries = [] qry = '"LOT_PLAN" = \'{0}\'' for rec in f: queries.append(qry.format(rec)) f.close() sQuery = " OR ".join(queries) print sQuery
import string ##Variables separated "find it easier to work with when doing query strings" lot_plan = 'LOT_PLAN' equal = '=' recs = ['ab1','ab2','ab3','ab4'] ##Set up string by concatenating each part begQuery = '\"%s" %s ' %(lot_plan,equal) ## Empty list to hold values query = [] ##May not need this but put it in anyway numRecs = len(recs) print numRecs for rec,i in zip(recs,range(numRecs)): totQuery = "%s '%s' OR" %(begQuery,rec) print totQuery query.append(totQuery) ## Then if you need it all in one string x = ' '.join(str(s) for s in query) print x
Thanks for that. I put your code in and run it, but it still comes up the same result.
Here is the updated code:Any idea?
>>> f = open("log") >>> for rec in f: ... rec ... '201211291133 0 0 0cprice ap\n' '201211291133 0 0 0cprice ae\n'
for rec in f: queries.append(qry.format(rec.strip()))
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.strip() + "' OR " else: sQuery = sQuery + "\"LOT_PLAN\" = '" + rec.strip() + "' " #print sQuery f.close() print sQuery