parsing a text file

701
3
08-04-2011 06:47 PM
IbraheemKhan1
New Contributor III
Hi Everyone,

I am new to python and my Q might sound trivial to many, sorry for that. I want to do automated geoprocessing based on some info in a text file. For that i require to automate the process of parsing the text file and subsequently get processing done based on the conditions defined in that text file. Any help on getting tips to parse a text file like 'reading specific text and using that as a condition' would be highly appreciated. Thanks for you time.

Thanks,
Ibe
Tags (2)
0 Kudos
3 Replies
ThomMackey
New Contributor III
Hi Ibe,

I'm afraid it's difficult to meaningfully help with such a broad question. Is there any chance you can provide an example of the specific operation you're trying to achieve?

If you're really brand new to the whole deal, I'd suggest the Python tutorial on Input/Output, specifically : section 7.2, "Reading and Writing Files".

If you can be more specific about what you're trying to do (eg. "Buffer records according to a value in a text file" or "Update attributes of a feature class based on a CSV file") then I'm sure we can all be more helpful 🙂

-Thom
0 Kudos
IbraheemKhan1
New Contributor III
Hi Thom,

Records in the file are pasted below:

proj = lcc
cell_x = 10
cell_y = 10
NoData = -9999

I want to read only 'cell_x' and 'cell_y' and write it to a separate file which will serve as input to geoprocessing tools in a model. Any suggestions.

Thanks,
Ibe
0 Kudos
RDHarles
Occasional Contributor
Something like this should work:

# Open text file for writing
outfile = open("cell.txt", "a")
# Open text file for reading (one line at a time).
for lines in open(infile.txt).readlines():                
    line = lines.strip()
    # Only lines that start with "cell_"
    if line.startswith('cell_'):
        outfile.write(line+"\n")
0 Kudos