|
POST
|
The link should show you all the formatting codes related to the timedate object. To display the date with month first and year last, just move %Y to the end. To mark a correct answer, you would need to change this "discussion" to a "question". Then a correct answer option would be available.
... View more
01-02-2020
10:50 AM
|
0
|
0
|
2468
|
|
POST
|
To change to a 4 digit year, just use an upper case "Y" in line 41: date1 = date1.strftime('_%Y-%m-%d') See strftime and strptime behavior for the datetime format codes.
... View more
01-01-2020
11:46 PM
|
0
|
2
|
2468
|
|
POST
|
Try replacing lines 39-42 in your latest code with (maintaining the indentation): date1 = datetime.fromtimestamp(os.path.getmtime(path1))
# date1 = str(date1) # delete or comment out - this line is not needed and is causing the error
date1 = date1.strftime('_%y-%m-%d') # this converts datetime object to a formatted string - add and underscore to format
os.rename(path1,archivedDirectory+"\\"+fileName+date1+extension) # previous line formats date1 to text The os.path.getmtime returns a timestamp which is converted by datetime.fromtimestamp into a datetime object ( year, month, day, hours, minutes, seconds). Since date1 is now a datetime object, strftime can be used to format it into a string "_yy-mm-dd". Once it is a string, it can be added into the file's name sequence: fileName+date1+extension. In my previous code snippet, I was renaming the file as it was being moved. In this snippet the file is renamed after it was moved. Either way should work for you.
... View more
01-01-2020
06:46 PM
|
0
|
4
|
7840
|
|
POST
|
In your code at line 9, you are getting the current date/time and using it in lines 32 and 36. I'm not sure your code inside the loop starting at line 22 is doing what you expect. But for now, just focusing on the date issue, I would suggest trying something like this to replace lines 29-36: if (os.path.exists(directory+"\\"+fileName+extension)):
modDate = datetime.fromtimestamp(os.path.getmtime(directory+"\\"+fileName+extension))
shutil.move(directory+"\\"+fileName+extension, archivedDirectory+"\\"+fileName+str(modDate)[:10]+extension) Back to the loop, at line 32, do you want to check if multiple files exist that have the current date in the file name? Hope this helps.
... View more
12-31-2019
08:01 PM
|
0
|
6
|
7840
|
|
POST
|
While experimenting with a line shape file, I realized that you do not need to convert the .dbf table to a text file to make use MakeXYEventLayer if it contains X and Y values. For my experiment, I created some lines and save them as a shape file. I used AddGeometryAttributes to add an XY value for the mid-point of the line. The .dbf and .prj files were used to create a point feature. Perhaps the test code will give you some ideas for your project. import arcpy, os
arcpy.env.workspace = r'C:\Users\Randy\Documents\ArcGIS\PythonScripts\test\rbTest'
arcpy.env.overwriteOutput = True
dbf = 'line_test.dbf' # shape file data table
sr = arcpy.SpatialReference(os.path.join(arcpy.env.workspace,'line_test.prj')) # shape file spatial reference
# print sr.name, sr.factoryCode
FieldX1 = 'MID_X' # midpoint of line feature
FieldY1 = 'MID_y'
FieldZ1 = None
outShape = 'point_test.shp'
points = arcpy.MakeXYEventLayer_management(dbf, FieldX1, FieldY1, 'points', sr, FieldZ1) # make a point layer
result = arcpy.FeatureClassToFeatureClass_conversion(points, arcpy.env.workspace, outShape)
print result
'''
# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "line_test"
CENTROID - Adds attributes to store the centroid coordinates of each feature.
(CENTROID_X, CENTROID_Y)
CENTROID_INSIDE - Adds attributes to store the coordinates of a central point inside or on each feature.
(INSIDE_X, INSIDE_Y)
LINE_START_MID_END - Adds attributes to store the coordinates of the start, mid, and end points of each feature.
(START_X, START_Y, MID_X, MID_Y, END_X, END_Y)
LINE_BEARING - Adds an attribute to store the start-to-end bearing of each line feature.
Values range from 0 to 360, with 0 meaning north, 90 east, 180 south, 270 west, and so on.
(BEARING)
arcpy.AddGeometryAttributes_management(
Input_Features="line_test",
Geometry_Properties="LINE_START_MID_END;CENTROID;CENTROID_INSIDE;LINE_BEARING",
Length_Unit="", Area_Unit="", Coordinate_System="")
''' Hope this helps.
... View more
12-15-2019
09:47 PM
|
0
|
0
|
631
|
|
POST
|
The basic idea is that a join could be made on the employee domain code in a feature layer to the employee code in an employee table. For example, a study to evaluate access to tobacco products by underage youth could use something similar to an employee table. The feature point layer would have known tobacco vendors and contain a domain for an underage youth and a yes-no domain for a successful tobacco purchase. The table for the youth would also contain age, gender and other relevant information. An analysis could be done to answer a question like where are underage girls more likely to access tobacco products and if the attempts were sufficiently gender neutral. For this, you would need somewhere to store the information about the youth; a table in the database is an option. Then a domain can be created from this table. But the project could also be organized in many other ways. This is just one of them.
... View more
12-13-2019
10:33 AM
|
1
|
0
|
2340
|
|
POST
|
The additional information is helpful. Could you share a sample of what the Mobilier_Urbain_L.shp file is like? Also, is it safe to assume that all your shape files use the same format? I noticed that the FieldX1 used in the MakeXYEventLayer may be the field name and probably should be quoted. I am assuming there is a field in the shape file with this name. If not, you would need to set it to the field's name, such as FieldX1 = 'XfieldName'. arcpy.MakeXYEventLayer_management(OutTxtMob, 'FieldX1', 'FieldY1', OutTempMob, ParemSpat, 'FieldZ1')
... View more
12-12-2019
09:46 AM
|
0
|
0
|
631
|
|
POST
|
Thank you for the additional description of your project. I am still trying to understand the work flow. Is arcpy.GetParameter(1) around line 5 fetching a list of shapefiles, such as [ 'Mobilier_Urbain_L.shp', 'M_U_L.shp', ....] ? And starting around line 15 to 19, is the .dbf file being converted to a text file and then into an XY layer file? Since the .dbf would be part of the shape file, is there a reason to do an XY conversion? Perhaps it would be better to converting the shape file to a feature layer with MakeFeatureLayer and then saving it with FeatureClassToFeatureClass? tmplyr = arcpy.MakeFeatureLayer_management(r'C:\path\to\shapefile.shp','tmplyr')
arcpy.FeatureClassToFeatureClass_conversion("tmplyr",r'C:\path\to\file.gdb','shp_conv') Thanks.
... View more
12-11-2019
09:54 PM
|
0
|
1
|
2461
|
|
POST
|
Can you describe what you want your script tool to do? A description of the tool's function may assist in getting an answer to your question. I would suggest that you do some testing of the code before putting it in a script tool. This will help debug the script as error messages will display and help pinpoint the source of the problem. I've retyped your code as Dan Patterson has suggested (using the syntax highlighter). This format will give some line numbers to aid in the discussion. import arcpy, math, os
arcpy.env.workspace = arcpy.GetParameter(0)
arcpy.env.overwriteOutput = True
ParemListFea = arcpy.GetParameter(1)
ParemSpat = arcpy.GetParameter(2)
try:
SpatRef = ParemSpat
SelectMob = arcpy.SelectLayerByAttribute_management(MobFeat, "NEW_SELECTION", "nom = '1105'")
ComMob = arcpy.GetCount_management(SelectMob)
MobFeat = "Mobilier_Urbain_L"
OutMob = "Mobilier0"
OutTempMob = "MobilierTemp"
Dbf_Mob = "Mobilier_Urbaine_L.dbf"
OutTxt_Mob = "Mobilier_U_L.txt"
if ListFeatIn == "Mobilier_Urbain_L.shp" and ComMob > 0:
arcpy.TableToTable_conversion(Dbf_mob, arcpy.env.workspace, OutTxt_Mob)
arcpy.MakeXYEventLayer_management(OutTxtMob, FieldX1, FieldY1, OutTempMob, ParemSpat, FieldZ1)
arcpy.FeatureClassToFeatureClass_conversion(OutTempMob, arcpy.env.workspace, OutMob) From the code snippet above, I notice that several variables are referenced before they are defined. In line 9, MobFeat is being referenced, but it is not defined until line 12. Perhaps line 12 should be moved before line 9 ? And in line 17 the value of ListFeatIn is being checked to see if it matches the name of a shape file. Perhaps ListFeatIn is supposed to be ParemListFea ? I do not see a reference to ParemListFea being used later in the script. Line 17 also references ConMob. This value will be zero or None if the SelectLayerByAttibute fails at line 9. The where clause in line 9 is referencing a field nom and checking for a certain text string '1105' that may be a number 1105. This will affect what is being returned by the query. These are just a few quick observations. Please tell us more about your project.
... View more
12-10-2019
08:43 PM
|
0
|
0
|
2461
|
|
POST
|
If you are using the Python API, see: arcgis.features.FeatureLayer.query and use return_count_only=True
... View more
12-09-2019
07:46 PM
|
1
|
0
|
2723
|
|
POST
|
See: Query (Feature Service) The Query operation is performed on a feature service resource. The result of this operation is either a feature set for each layer in the query or a count of features for each layer (if returnCountOnly is set to true) or an array of feature IDs for each layer in the query (if returnIdsOnly is set to true). I did a quick test, with returnCountOnly set to true and using a where "1=1". It returned a count. URL = "https://services.arcgis.com/xxxxx/arcgis/rest/services/myFeature/FeatureServer/0/query"
query_dict = {
"where" : "1=1",
"returnCountOnly" : "true",
"returnGeometry" : "false",
"f": "json", "token": token['token'] }
jsonResponse = urllib.urlopen(URL, urllib.urlencode(query_dict))
response = json.loads(jsonResponse.read())
print response
# prints: {u'count': 486, u'serverGens': {u'serverGen': 461482, u'minServerGen': 65364}}
... View more
12-09-2019
01:50 PM
|
0
|
0
|
2723
|
|
POST
|
Just confirming, you tried placing the startOperation before the UpdateCursor? edit = arcpy.da.Editor(sdeconnection)
edit.startEditing(False, True)
edit.startOperation()
with arcpy.da.UpdateCursor(relatedtable, ["ID", "Plot"]) as cursor:
for row in cursor:
gid = row[0]
for key, value in plotdict.iteritems():
if gid == key:
row[1] = value
cursor.updateRow(row)
edit.stopOperation()
edit.stopEditing(True)
... View more
12-04-2019
02:12 PM
|
0
|
1
|
4586
|
|
POST
|
Outside of ArcMap, I don't think "Current" will work as a map document. Try using a full path/filename to your map document in line 5. Except for this error, it appears that you have Pythonwin working, correct?
... View more
12-02-2019
09:33 AM
|
1
|
0
|
1752
|
|
POST
|
Another way: fc = 'feature'
fields = ['Facility1', 'Facility2', 'Facility3', 'MostVisited'] # field names
facility = ['One', 'Two', 'Three' ] # facility names
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
row[3] = facility[row[:3].index(max(row[:3]))]
# row[3] is the 'MostVisited' field
# row[:3] are the first 3 fields (visit counts)
cursor.updateRow(row) # this would do the update
... View more
11-29-2019
09:58 AM
|
1
|
1
|
2307
|
|
POST
|
Have you checked out: Turbo Charging Data Manipulation with Python Cursors and Dictionaries ? From your description, it sounds like you want to do what is described in the article.
... View more
11-27-2019
11:43 AM
|
0
|
0
|
2516
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 10-27-2016 02:23 PM | |
| 1 | 09-09-2017 08:27 PM | |
| 2 | 08-20-2020 06:15 PM | |
| 1 | 10-21-2021 09:15 PM | |
| 1 | 07-19-2018 12:33 PM |
| Online Status |
Offline
|
| Date Last Visited |
02-12-2026
07:13 PM
|