Skip to main content
added 8 characters in body
Source Link
geoJshaun
  • 1.3k
  • 1
  • 16
  • 34
        arcpy.FeatureClassToFeatureClass_conversion(layer, outputWS, finalFC)[0]
        outPath = 'S:\\Shared\\RouteSmart\\CO_161_Cleanscapes\\To_Tower'
        name = os.path.basename(finalFC)
        
        dataFields = [i.name for i in arcpy.ListFields(finalFC)]

        file = os.path.join(outPath, ("{0}{1}{2}".format(day,name,".txt")))
        print file
        f = open(file, "w")

        writeFields = []

        for field in inFields:
            if field in dataFields:
                writeFields.append(field)

        for row in arcpy.SearchCursor(finalFC):
            # No longer need to reference name property (field.name)
            fieldVals = [row.getValue(field) for field in writeFields]
            # Replace nulls with empty strings
            fieldsVals = ['' if i is None else i for i in fieldVals]
            # Create a comma-separated string of values
            fieldVals = [str(field) for field in fieldVals]
            out_string = ','.join(fieldVals)
            # Write the string--not the list--to the table
            f.writelines(out_string)
        del row
        f.close()

    arcpy.Delete_management(layer)
    arcpy.Delete_management("OldSeq")
arcpy.FeatureClassToFeatureClass_conversion(layer, outputWS, finalFC)[0]
        outPath = 'S:\\Shared\\RouteSmart\\CO_161_Cleanscapes\\To_Tower'
        name = os.path.basename(finalFC)
        
        dataFields = [i.name for i in arcpy.ListFields(finalFC)]

        file = os.path.join(outPath, ("{0}{1}{2}".format(day,name,".txt")))
        print file
        f = open(file, "w")

        writeFields = []

        for field in inFields:
            if field in dataFields:
                writeFields.append(field)

        for row in arcpy.SearchCursor(finalFC):
            # No longer need to reference name property (field.name)
            fieldVals = [row.getValue(field) for field in writeFields]
            # Replace nulls with empty strings
            fieldsVals = ['' if i is None else i for i in fieldVals]
            # Create a comma-separated string of values
            fieldVals = [str(field) for field in fieldVals]
            out_string = ','.join(fieldVals)
            # Write the string--not the list--to the table
            f.writelines(out_string)
        del row
        f.close()

    arcpy.Delete_management(layer)
    arcpy.Delete_management("OldSeq")
        arcpy.FeatureClassToFeatureClass_conversion(layer, outputWS, finalFC)[0]
        outPath = 'S:\\Shared\\RouteSmart\\CO_161_Cleanscapes\\To_Tower'
        name = os.path.basename(finalFC)
        
        dataFields = [i.name for i in arcpy.ListFields(finalFC)]

        file = os.path.join(outPath, ("{0}{1}{2}".format(day,name,".txt")))
        print file
        f = open(file, "w")

        writeFields = []

        for field in inFields:
            if field in dataFields:
                writeFields.append(field)

        for row in arcpy.SearchCursor(finalFC):
            # No longer need to reference name property (field.name)
            fieldVals = [row.getValue(field) for field in writeFields]
            # Replace nulls with empty strings
            fieldsVals = ['' if i is None else i for i in fieldVals]
            # Create a comma-separated string of values
            fieldVals = [str(field) for field in fieldVals]
            out_string = ','.join(fieldVals)
            # Write the string--not the list--to the table
            f.writelines(out_string)
        del row
        f.close()

    arcpy.Delete_management(layer)
    arcpy.Delete_management("OldSeq")
to demonstrate what's working to others
Source Link
geoJshaun
  • 1.3k
  • 1
  • 16
  • 34

After taking a mix tips from the answers I've gotten it close. The None's are still there but that's the only thing left to resolve. Here's the modified code:

arcpy.FeatureClassToFeatureClass_conversion(layer, outputWS, finalFC)[0]
        outPath = 'S:\\Shared\\RouteSmart\\CO_161_Cleanscapes\\To_Tower'
        name = os.path.basename(finalFC)
        
        dataFields = [i.name for i in arcpy.ListFields(finalFC)]

        file = os.path.join(outPath, ("{0}{1}{2}".format(day,name,".txt")))
        print file
        f = open(file, "w")

        writeFields = []

        for field in inFields:
            if field in dataFields:
                writeFields.append(field)

        for row in arcpy.SearchCursor(finalFC):
            # No longer need to reference name property (field.name)
            fieldVals = [row.getValue(field) for field in writeFields]
            # Replace nulls with empty strings
            fieldsVals = ['' if i is None else i for i in fieldVals]
            # Create a comma-separated string of values
            fieldVals = [str(field) for field in fieldVals]
            out_string = ','.join(fieldVals)
            # Write the string--not the list--to the table
            f.writelines(out_string)
        del row
        f.close()

    arcpy.Delete_management(layer)
    arcpy.Delete_management("OldSeq")

After taking a mix tips from the answers I've gotten it close. The None's are still there but that's the only thing left to resolve. Here's the modified code:

arcpy.FeatureClassToFeatureClass_conversion(layer, outputWS, finalFC)[0]
        outPath = 'S:\\Shared\\RouteSmart\\CO_161_Cleanscapes\\To_Tower'
        name = os.path.basename(finalFC)
        
        dataFields = [i.name for i in arcpy.ListFields(finalFC)]

        file = os.path.join(outPath, ("{0}{1}{2}".format(day,name,".txt")))
        print file
        f = open(file, "w")

        writeFields = []

        for field in inFields:
            if field in dataFields:
                writeFields.append(field)

        for row in arcpy.SearchCursor(finalFC):
            # No longer need to reference name property (field.name)
            fieldVals = [row.getValue(field) for field in writeFields]
            # Replace nulls with empty strings
            fieldsVals = ['' if i is None else i for i in fieldVals]
            # Create a comma-separated string of values
            fieldVals = [str(field) for field in fieldVals]
            out_string = ','.join(fieldVals)
            # Write the string--not the list--to the table
            f.writelines(out_string)
        del row
        f.close()

    arcpy.Delete_management(layer)
    arcpy.Delete_management("OldSeq")
edited title
Link
ahmadhanb
  • 41.4k
  • 5
  • 54
  • 109

writing Writing to a text file from arcpy.ListFields()

edited tags
Link
geoJshaun
  • 1.3k
  • 1
  • 16
  • 34
Loading
Source Link
geoJshaun
  • 1.3k
  • 1
  • 16
  • 34
Loading