Select to view content in your preferred language

ArcGis reading field as integer when it should be string.

913
7
Jump to solution
11-07-2012 10:36 AM
FrankChin
New Contributor
Hello,
I am trying to read in a .txt (tab delimited) file using a python script and ArcGIS 10.1:

SCRB_ZIP_5_CD SCRB_ZIP_4_CD SCRB_ZIP_3_CD SCRB_ST_CD COUNTY_FIPS_CD LAT_DEG LNG_DEG NEW_RECORD_IND ID
"02916" "1245" "703" "RI" "44007" 41.843590 -71.349532 "N" 50

The first field, SCRB_ZIP_5_CD, should be a string, however it is being read as an integer and dropping the leading zero, is there a workaround to read this field as a string?

Any help would be appreciated.

Thanks,
Frank
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KimOllivier
Occasional Contributor III
You have not specified how you are reading in the text file.

Have you had a look at the schema.ini specification? This is a Microsoft standard for defining the schema for reading in text files.
It works really well for loading text files into a geodatabase table because you can rename, set input widths, types to make the simple table to table tool work very well. This avoids a lot of complex coding opening a file and unpicking the components.

If you have a table in a database try exporting it as a txt file and at 10.x a schema.ini file will be generated for you. Then keep the file with the source, edit the name and hey presto a schema that works.

Here is an example where I actually write the schema after dumping out the table as a text file. This is much faster than an InsertCursor at 10.0. Maybe the new da module makes it faster, but it is still a very easy way of defining a schema.

..... f2 = open("e:/crs/currentgdb/schema.ini","w") f2.write("""[owners.csv] Format=CSVDelimited ColNameHeader=True col1=TTL_TITLE_NO Text Width 20 col2=OWNERS Text Width 120""") f2.close() print "uses schema.ini" arcpy.conversion.TableToTable(csvOwners,ws,"owners")

View solution in original post

0 Kudos
7 Replies
ChristopherBlinn1
Occasional Contributor III
Can you convert the value to a string before using it in the script?

str(field value) will return the value as type string.  You can create a variable to equal the string value then use the variable in the script.

Hope this helps!
Chris B.
0 Kudos
T__WayneWhitley
Frequent Contributor
Interesting...not sure and I don't remember offhand without testing, but if you read the line into a python list, does this behavior exhibit?  Or can you read directly into a text variable, something like var = '' (not var = 0), then load the variable, and print it.  Also, if you're allowed, it may suffice to to format your input file, quoting the vals that are meant to be text.
Hope that helps...
0 Kudos
KimOllivier
Occasional Contributor III
You have not specified how you are reading in the text file.

Have you had a look at the schema.ini specification? This is a Microsoft standard for defining the schema for reading in text files.
It works really well for loading text files into a geodatabase table because you can rename, set input widths, types to make the simple table to table tool work very well. This avoids a lot of complex coding opening a file and unpicking the components.

If you have a table in a database try exporting it as a txt file and at 10.x a schema.ini file will be generated for you. Then keep the file with the source, edit the name and hey presto a schema that works.

Here is an example where I actually write the schema after dumping out the table as a text file. This is much faster than an InsertCursor at 10.0. Maybe the new da module makes it faster, but it is still a very easy way of defining a schema.

..... f2 = open("e:/crs/currentgdb/schema.ini","w") f2.write("""[owners.csv] Format=CSVDelimited ColNameHeader=True col1=TTL_TITLE_NO Text Width 20 col2=OWNERS Text Width 120""") f2.close() print "uses schema.ini" arcpy.conversion.TableToTable(csvOwners,ws,"owners")
0 Kudos
FrankChin
New Contributor
Thank you for the suggestions.  I ended up using the schema.ini method and that worked fine.
0 Kudos
T__WayneWhitley
Frequent Contributor
Thank you Kim Ollivier- I forgot all about that, think that counts as having learned something new today.

By the way, I used some of your python script samples - for example, not sure off the top of my head where I found it, but used something you wrote to recalc DDP index grid page angles...good show, nice attention to detail!  (I pondered over your comment 'careful with up' - I got that after running an experimental strip around 'steep curves' and watched the angle measure 'flop'.  Sometimes 'up' is 'down'.)
0 Kudos
RyanSchuermann
New Contributor
You have not specified how you are reading in the text file.

Have you had a look at the schema.ini specification? This is a Microsoft standard for defining the schema for reading in text files.
It works really well for loading text files into a geodatabase table because you can rename, set input widths, types to make the simple table to table tool work very well. This avoids a lot of complex coding opening a file and unpicking the components.



I ran across this looking for an issue with TabletoTable appearing to not read my csv header row properly. It was forcing only some of the field names in only one of my csv files to be "Field5" and "Field6" ...very weird.

Sure enough, I had somehow generated a schema.ini file that was defining it! It was just doing what it was told to do 🙂

thanks for pointing out the existence of this file and how to use it!
0 Kudos
curtvprice
MVP Esteemed Contributor
Help reference -

Desktop Help 10.1: Adding an ASCII or text file table


See the section: Overriding how text files are formatted
0 Kudos