Identify and delete all unrequired fields from Shape file using Arcpy
I have some Shape files inside one Folder and for the sake of datacompression I want to delete all fields from Table of Content that can be removed (that are not required)!
What I have:
What I expect:
My code that is not working:
import arcpy
import os
arcpy.env.workspace = r"C:Temp"
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
fields = arcpy.ListFields(fc)
for field in fields:
delFlds = [field for field in fields if field != "FID" or field != "Shape"]
arcpy.DeleteField_management(fc, delFlds)
I am using python 2.7 and ArcMap 10.4.1
arcpy shapefile arcgis-10.4
New contributor
add a comment |
I have some Shape files inside one Folder and for the sake of datacompression I want to delete all fields from Table of Content that can be removed (that are not required)!
What I have:
What I expect:
My code that is not working:
import arcpy
import os
arcpy.env.workspace = r"C:Temp"
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
fields = arcpy.ListFields(fc)
for field in fields:
delFlds = [field for field in fields if field != "FID" or field != "Shape"]
arcpy.DeleteField_management(fc, delFlds)
I am using python 2.7 and ArcMap 10.4.1
arcpy shapefile arcgis-10.4
New contributor
add a comment |
I have some Shape files inside one Folder and for the sake of datacompression I want to delete all fields from Table of Content that can be removed (that are not required)!
What I have:
What I expect:
My code that is not working:
import arcpy
import os
arcpy.env.workspace = r"C:Temp"
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
fields = arcpy.ListFields(fc)
for field in fields:
delFlds = [field for field in fields if field != "FID" or field != "Shape"]
arcpy.DeleteField_management(fc, delFlds)
I am using python 2.7 and ArcMap 10.4.1
arcpy shapefile arcgis-10.4
New contributor
I have some Shape files inside one Folder and for the sake of datacompression I want to delete all fields from Table of Content that can be removed (that are not required)!
What I have:
What I expect:
My code that is not working:
import arcpy
import os
arcpy.env.workspace = r"C:Temp"
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
fields = arcpy.ListFields(fc)
for field in fields:
delFlds = [field for field in fields if field != "FID" or field != "Shape"]
arcpy.DeleteField_management(fc, delFlds)
I am using python 2.7 and ArcMap 10.4.1
arcpy shapefile arcgis-10.4
arcpy shapefile arcgis-10.4
New contributor
New contributor
edited 8 hours ago
BERA
15.7k52042
15.7k52042
New contributor
asked 8 hours ago
gisgisgisgis
133
133
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You are listing field objects with ListFields, not the field names which should be input to DeleteField. So change:
[field for field in fields if field != "FID" or field != "Shape"]
to:
[field.name for field in fields if field.name not in ("FID","Shape")]
But it is probably better to use the required
property since object id and shape fields can have different names in different feature classes:
for fc in fcList:
fields_to_delete = [field.name for field in arcpy.ListFields(fc) if not field.required]
fields_to_delete.pop() #Keep one non-required field
for field in fields_to_delete:
arcpy.DeleteField_management(fc, field)
Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."
– gisgis
8 hours ago
@gisgis ok you need to keep one then. I edited my answer.
– BERA
8 hours ago
1
perfect it worked! Thanks a lot.
– gisgis
8 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
gisgis is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f311490%2fidentify-and-delete-all-unrequired-fields-from-shape-file-using-arcpy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are listing field objects with ListFields, not the field names which should be input to DeleteField. So change:
[field for field in fields if field != "FID" or field != "Shape"]
to:
[field.name for field in fields if field.name not in ("FID","Shape")]
But it is probably better to use the required
property since object id and shape fields can have different names in different feature classes:
for fc in fcList:
fields_to_delete = [field.name for field in arcpy.ListFields(fc) if not field.required]
fields_to_delete.pop() #Keep one non-required field
for field in fields_to_delete:
arcpy.DeleteField_management(fc, field)
Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."
– gisgis
8 hours ago
@gisgis ok you need to keep one then. I edited my answer.
– BERA
8 hours ago
1
perfect it worked! Thanks a lot.
– gisgis
8 hours ago
add a comment |
You are listing field objects with ListFields, not the field names which should be input to DeleteField. So change:
[field for field in fields if field != "FID" or field != "Shape"]
to:
[field.name for field in fields if field.name not in ("FID","Shape")]
But it is probably better to use the required
property since object id and shape fields can have different names in different feature classes:
for fc in fcList:
fields_to_delete = [field.name for field in arcpy.ListFields(fc) if not field.required]
fields_to_delete.pop() #Keep one non-required field
for field in fields_to_delete:
arcpy.DeleteField_management(fc, field)
Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."
– gisgis
8 hours ago
@gisgis ok you need to keep one then. I edited my answer.
– BERA
8 hours ago
1
perfect it worked! Thanks a lot.
– gisgis
8 hours ago
add a comment |
You are listing field objects with ListFields, not the field names which should be input to DeleteField. So change:
[field for field in fields if field != "FID" or field != "Shape"]
to:
[field.name for field in fields if field.name not in ("FID","Shape")]
But it is probably better to use the required
property since object id and shape fields can have different names in different feature classes:
for fc in fcList:
fields_to_delete = [field.name for field in arcpy.ListFields(fc) if not field.required]
fields_to_delete.pop() #Keep one non-required field
for field in fields_to_delete:
arcpy.DeleteField_management(fc, field)
You are listing field objects with ListFields, not the field names which should be input to DeleteField. So change:
[field for field in fields if field != "FID" or field != "Shape"]
to:
[field.name for field in fields if field.name not in ("FID","Shape")]
But it is probably better to use the required
property since object id and shape fields can have different names in different feature classes:
for fc in fcList:
fields_to_delete = [field.name for field in arcpy.ListFields(fc) if not field.required]
fields_to_delete.pop() #Keep one non-required field
for field in fields_to_delete:
arcpy.DeleteField_management(fc, field)
edited 1 hour ago
answered 8 hours ago
BERABERA
15.7k52042
15.7k52042
Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."
– gisgis
8 hours ago
@gisgis ok you need to keep one then. I edited my answer.
– BERA
8 hours ago
1
perfect it worked! Thanks a lot.
– gisgis
8 hours ago
add a comment |
Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."
– gisgis
8 hours ago
@gisgis ok you need to keep one then. I edited my answer.
– BERA
8 hours ago
1
perfect it worked! Thanks a lot.
– gisgis
8 hours ago
Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."
– gisgis
8 hours ago
Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."
– gisgis
8 hours ago
@gisgis ok you need to keep one then. I edited my answer.
– BERA
8 hours ago
@gisgis ok you need to keep one then. I edited my answer.
– BERA
8 hours ago
1
1
perfect it worked! Thanks a lot.
– gisgis
8 hours ago
perfect it worked! Thanks a lot.
– gisgis
8 hours ago
add a comment |
gisgis is a new contributor. Be nice, and check out our Code of Conduct.
gisgis is a new contributor. Be nice, and check out our Code of Conduct.
gisgis is a new contributor. Be nice, and check out our Code of Conduct.
gisgis is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f311490%2fidentify-and-delete-all-unrequired-fields-from-shape-file-using-arcpy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown