1

I am writing a python code in order to bring my.txt file into a dbf file in arcgis. I am using CopyRows_management which seemed to be working just fine until I realized that one of the 40 fields from the txt file loses the leading zeros of number(it is the only field with leading zeros with sometimes just one "0", somtimes two "0", sometimes none). Is there a way to keep these zeros?

Any help will be welcome.

Thanks so much.

user29458
  • 13
  • 2
  • This sounds more like a Python rather than GIS (ArcPy/ArcGIS) problem so can you include a working code snippet to both make what you are asking clear and to demonstrate that it is on topic for this site, please? – PolyGeo Apr 22 '14 at 20:08

2 Answers2

1

You can edit the schema.ini file that should be located in the same folder as your txt file and specify that the field with leading zeros should be treated as text rather letting it assume it's an integer (which is why you're losing the zeros). Or if there isn't a schema.ini file, you can create it (Notepad works just fine in this instance). You need to figure out what number is of the column that is losing the zeros (starting with the left-most column being Col1). Let's say it's the 12th column that's losing the zeros, and that the name of that column is FooBar. Replacing example.txt with the actual name of your text file, the contents of the file could look something like this.

[example.txt]
ColNameHeader=True
Col12=FooBar Text

Once you've edited/created schema.ini, save it. If you have ArcMap open you'll need to restart it. You can check to see if things are working you can add the txt file to the table of contents in ArcMap, right click the problem column and check the field type, which should now read Text and all the leading zeros should be preserved.

See this question and this one for more info.

Kevin R Dyke
  • 851
  • 4
  • 9
0

Have a look at the dbfpy module.

You'll find same basic usage examples on their website.

Important in your case is the addField method. To store numerical values use

object.addField( 
    ("FieldName", "N", 20), # N stands for numerical
    ... )
LarsVegas
  • 2,536
  • 3
  • 30
  • 47