5

I am having a strange problem with the ArcPy Table to Table function. For some reason, when I use the function to create a CSV file in a folder that exists outside of my python project folder, several columns in the CSV file have their headers cut short. For example, I have a column titled "PASSING_400_MICROM" that gets truncated to "PASSING_400_MICR'.

Thus far, I have discovered the following about the problem:

  1. The truncation of the same column headers appear to occur in any folder, both on my shared network and local machine, that isn't the python project folder. All the headers are truncated to 16 characters in length.
  2. No truncation occurs if I place the CSV within my project file. (However, I can't do this for the final code, as people on other computers will use my code)

I've done a lot of searching, and I still don't understand why this is happening and how to fix it. I am relatively new to Python and ArcGIS, so I don't even know if I'm searching for the correct things.

What might be causing this problem, and how I can fix it?

Here is an example of the function I am using:

arcpy.TableToTable_conversion(in_rows=DatabaseLocation + '\\Table',
 out_path=CSVLocation,
 out_name='Table.csv',
 where_clause=whereclause)

The same thing happens when I run the tool from it's dialog.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Feb 5, 2019 at 22:08
6
  • 1
    Does it have to be a CSV file, you could export to Excel format? Commented Feb 5, 2019 at 22:15
  • I would prefer to use CSV format if possible: it makes some of the things I do later on in the code a lot easier. But thank you for your Idea! I may use that if I can't figure out this problem. Commented Feb 5, 2019 at 22:18
  • What happens if you run the same tool from its dialog with the same parameters? Commented Feb 5, 2019 at 22:31
  • 2
    Does the same thing happen with Table Select resources.arcgis.com/en/help/main/10.2/index.html#//… ? It is probable that there is an intermediate DBF file created; if the same thing happens with this tool I would go for user2856's solution feeding in [ x.name for x in arcpy.ListFields(os.path.join(DatabaseLocation, 'Table')) ] to get all fields in the table as the fields parameter. Commented Feb 5, 2019 at 23:34
  • @PolyGeo Yes, the same thing happens when I run the tool from it's dialog. I think I will use a different method, like others have suggested. Commented Feb 6, 2019 at 16:02

2 Answers 2

5

As a workaround, you could write out the csv file yourself:

import os, csv
import arcpy
def table_to_csv(table, csv_file, fields, where_clause=None):
 """ Example to export a table or feature class attribute table to CSV"""
 with open(csv_file, 'wb') as csv_file_obj, arcpy.da.SearchCursor(table, fields, where_clause=where_clause) as rows:
 csv_writer = csv.writer(csv_file_obj)
 csv_writer.writerow(rows.fields)
 for row in rows:
 csv_writer.writerow(row)
# Rest of your code etc...
# arcpy.TableToTable_conversion(in_rows=DatabaseLocation + '\\Table',
# out_path=CSVLocation,
# out_name='Table.csv',
# where_clause=whereclause)
table_to_csv(os.path.join(DatabaseLocation, 'Table'), 
 os.path.join(CSVLocation,'Table.csv'), 
 ['field1', 'field2'],
 whereclause)
answered Feb 5, 2019 at 23:14
3
  • +1 If you have any dates, you might need to write a schema.ini too. Commented Feb 6, 2019 at 1:23
  • Thank you for your suggestion! I tried to run the code however, and I kept getting this error: TypeError: SearchCursor() got multiple values for keyword argument 'where_clause' . Thoughts? I looked it up, and in some forums it had something to do with not putting "self" as an argument, but I don't know if that applies in this function. Commented Feb 6, 2019 at 16:04
  • 1
    @TheQueryingIntern I can't reproduce that. If you're using this code exactly as posted (i.e as a standalone function, not part of a class method - self is only relevant for class methods) it's not possible to get that exception. The only way to get a similar exception (TypeError: table_to_csv() got multiple values for keyword argument 'where_clause' note "table_to_csv()" not "SearchCursor") is to call it with TWO where_clause arguments, one as a positional argument and one a keyword argument, i.e table_to_csv(table, csvfile, ["field"], "ID=123", where_clause="ID=456") etc... Commented Feb 7, 2019 at 22:47
2

I just tried this in 10.6.1 running on Windows 10. I added a test field called "PASSING_400_MICROM" and set all values to be "YES". When I went to tun the Table to Table tool and set the output to be a CSV file it truncated the field name to 11 characters. I tried editing the field map and it set it back to 11 characters.

My gut feeling is that this tool is imposing some sort of field name limitation, like it or not.

At this point I would suggest you head over to the ESRI Code Sharing website, the first place of call for user created tools, be it from the community at large or an ESRI development team. I have used the Excel and CSV Conversion Toolbox when needing to convert to CSV because of the flakiness of the Table to Table tool.

answered Feb 5, 2019 at 22:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.