Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to modify a VB-Expression saved in a cal-file by ArcGIS Desktop 9.3:

dim out
if [D19830701]=-9999 then
    out= [D19830501]
    out= [D19830701] 
end if

with this simple script:

import os, sys
o = open("D:\\","a") #open for append
for line in open("D:\\"):
    line = line.replace("D19830701","D19830801")
    o.write(line + "\n")

but all i get is this:

d i m   o u t 

 i f   [ D 1 9 8 3 0 7 0 1 ] = - 9 9 9 9   t h e n 

     o u t =   [ D 1 9 8 3 0 5 0 1 ] 

 e l s e 

     o u t =   [ D 1 9 8 3 0 7 0 1 ]   

 e n d   i f 

 _ _ e s r i _ f i e l d _ c a l c u l a t o r _ s p l i t t e r _ _ 

 o u t 

No Replacement, the script just added "spaces" between every letter.
I want to use this Replacement-Script to fit the cal-file for several calculations i need to make.

Is there maybe a problem with the encoding (unicode?) when the script opens the file?
Maybe somebody has had the same problem ;)
or a nice workaround.

share|improve this question
up vote 4 down vote accepted

To demonstrate consider the following test.txt file



more stuff


consider the following Python code

infile = "c:\\temp\\test.txt"
outfile = "c:\\temp\\testout.txt"
input = open(infile,"r")    #open for append
output = open(outfile, "w") #open for write
for line in input.readlines():
  print line
  outline = line.replace("D19830701","D19830801")

and the resultant output (for the life of me I can't get code single spaced so ignore the extra empty lines).



more stuff


It is safe since it maintains your existing code and readline has been replaced with readlines since a string is an iterable, readlines reads the whole line at once.

share|improve this answer
That code is bringing me one step closer: There are no empty lines in between. But there are still empty spaces between every letter....and no replacement :( when i load the VB-Code in the Field-Calculator, there are Chinese symbols ?! – Markus Jul 9 '11 at 10:49
UPDATE: The code works for a standard txt file, which i created with Notepad++!! but i still can't open the file with the Field Calculator in ArcGis 9 (chinese symbols)! It seems that is has something to do with the encoding/decoding (i'm not really in to that), as @Jason-Scheirer said before. – Markus Jul 10 '11 at 10:08

Is python a requirement? The .cal files are plain text that can be changed by a text editor. I've used Notepad++ to edit them before. Notepad++, or other free tools, have the capability to replace text strings in a given file, or in files found in a specified folder (all files or a subset that match specific filters).

share|improve this answer
Yap. Is a requirement. I need to replace the Word automatically out of a bigger script. – Markus Jul 6 '11 at 19:29

The saved script is UTF-16. Set the file's encoding in your favorite text editor and you should be able to edit it.

share|improve this answer
I need to know how to force Python to save a file back to "UTF-16", otherwise Field-Calculator can't use it. Any Idea? – Markus Jul 10 '11 at 10:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.