0

I am using ArcGIS Desktop: ArcMap 10.6 with ArcPy Library (Python 2.7.14).

Is there a way to export a second .csv file, named 'Points2.csv' with different fields, ie csv_fields_1 = ['5', '6', '7' '8'] within the same snippet?

2
  • 1
    Sure, build a function into your code. w3schools.com/python/python_functions.asp – GBG Mar 1 at 17:15
  • Anything that needs to run for both CSVs goes into the function, then any variable that needs to change (input path, output path, list of field names, etc.) becomes an argument – mikewatt Mar 1 at 18:05
1

You can put a list inside the list csv_fields = ['1', '2', '3', '4'], doint some like this:

csv_fields = [['1', '2', '3', '4'], ['5','6', '7', '8']]

and put a for outside of the principal loop:

out_dataset = 'Data'
arcpy.Sort_management(points, out_dataset, [[fields[11], 'ASCENDING']])
arcpy.management.CreateFolder('C:\\Users\\Me\\Desktop', 'Points_Table_Export')
outLoc = 'C:\\Users\\Me\\Desktop\\Points_Table_Export'
fms = arcpy.FieldMappings()
csv_fields_list = [['1', '2', '3', '4'], ['5','6', '7', '8']]
for csv_fields in csv_fields_list:
    for field in csv_fields:
        fm = arcpy.FieldMap()
        fm.addInputField(out_dataset, field)
        fms.addFieldMap(fm)
    arcpy.TableToTable_conversion(out_dataset, outLoc, 'Temp.csv', '', fms)
    df = pd.read_csv(outLoc + '\\Temp.csv')

    # Removes Object_ID from first column
    first_column = df.columns[0]
    df = df.drop([first_column], axis=1)    
    df.to_csv(outLoc + '\\Points.csv', index=False, header=False)   
    # Delete the temp csv
    os.remove(outLoc + '\\Temp.csv')
    os.remove(outLoc + '\\Temp.txt.xml')
    os.remove(outLoc + '\\schema.ini')
0

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.