1

I would like to export a table from a geodatabase to a csv file with the ConvertTableToCsvFile tool. I have read through the esri help file and adapted the code for my purpose:

import arcpy, os

### Set project path
project_path = r"C:\Users\..."
arcpy.env.overwriteOutput = 1

### Set current directory
gdb = project_path + "data/raw/ACS/ACS_2014_5YR_BG_06_CALIFORNIA.gdb/" 
out = project_path + "data/ACS/"

### Convert to csv
    arcpy.ConvertTableToCsvFile_locref(gdb + "X14_SCHOOL_ENROLLMENT", out + "X14_SCHOOL_ENROLLMENT", "COMMA")

However, I get the error that

the 'module' object has no attribute 'ConvertTableToCsvFile_locref'.

What do I do wrong? I have looked for similar questions but apparently the problem is so trivial that noone would ask. (I am still a beginner with ArcPy and Python.)

  • 1
    What ArcGIS version are you running? – Midavalo Feb 5 '17 at 21:35
  • ArcMap 10.4. I have checked that it is not about using locref/roads. – eigenvector Feb 5 '17 at 21:50
  • 1
    Have you considered using arcpy.TableToTable_conversion() or 'arcpy.TableToExcel_conversion()'? Table to Table will export to CSV – Midavalo Feb 5 '17 at 22:32
  • Thanks, this worked perfectly and solved my problem. I will update my question with the code that got it working for me now. If that's considered inappropriate, please tell me and I will remove it. Thank you again for your help. – eigenvector Feb 5 '17 at 22:44
  • 1
    Rather than modifying your question, you can post as an answer – Midavalo Feb 5 '17 at 22:45
2

The Convert Table to CSV File tool requires the Esri Roads and Highways extension and the Location Referencing toolbox.

If you do not have this extension enabled and the toolbox installed, arcpy will report that it can't find the tool.

the 'module' object has no attribute 'ConvertTableToCsvFile_locref'.

The _locref at the end of the tool name is referring to the Location Referencing toolbox.

  • 2
    Also, the alias depends on the version, i.e. this tool's alias @ 10.3 is 'roads' (Roads and Highways toolbox). – T. Wayne Whitley Feb 5 '17 at 18:08
  • This sounds like the solution to my problem, so thank you. I installed and enabled the Roads and Highway Extension but I have not managed to find the location referencing toolbox (see screenshot edited in post). How can I install that? Sorry for so many beginner questions. – eigenvector Feb 5 '17 at 20:02
  • @eigenvector I recommend you take out your edit above and use this to create a new question to find a solution for where to find that toolbox. – Midavalo Feb 5 '17 at 22:10
2

While Midavalo's answer is the correct response to the question posed by me, the following workaround solves the problem:

Instead of arcpy.ConvertTableToCsvFile_locref() it is much easier to use the arcpy.TableToTable_conversion() tool instead. The revised code works perfectly:

import arcpy, os

### Set project path
project_path = r"C:\Users\..."
arcpy.env.overwriteOutput = 1

### Set current directory
gdb = project_path + "data/raw/ACS/ACS_2014_5YR_BG_06_CALIFORNIA.gdb/" 
out = project_path + "data/ACS/"
arcpy.TableToTable_conversion(gdb + "X14_SCHOOL_ENROLLMENT", out, "X14_SCHOOL_ENROLLMENT.csv")

Credits again to Midavalo for the suggestion to use the table to table conversion tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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