我没有编写此代码,也没有真正使用Python,所以请耐心等待。下面的代码应该在我们的一个数据库上运行更新。创建一些新文件。但是,当我尝试运行它时,我得到上面的错误。我认为它可以达到某一点:
import arcpy
import os
import csv
import pandas as pd
import xlwt
import glob
##set workspaces
os.chdir("//dotatufs04/crm/Public/temp/DDahms/Python/Update_MRS")
In_shp = "DraftHistoricProjectFootprints.gdb/DraftHistoricProjectFootprints"
##delete columns on Production_dot_project
os.system(r"\\dotatufs04\crm\Public\temp\DDahms\Python\Update_MRS\Scripts\run_macro_delete_columns.vbs")
##make joined shapefile and update status
try:
# Set environment settings
arcpy.env.workspace = "//dotatufs04/crm/Public/temp/DDahms/Python/Update_MRS"
arcpy.env.qualifiedFieldNames = False
# Set local variables
layerName = "shapefile2"
joinTable = "PRODUCTION_DOTPF_PROJECT.xls/Sheet3$"
layerName2 = "tableview"
joinField1 = "AKSAS_Num_Txt"
joinField2 = "AKSAS_NUMBER"
join_tview = "test_tview"
output_shp = "DraftHistoricProjectFootprints.gdb/joined"
# Create a feature layer from the vegtype featureclass
arcpy.MakeFeatureLayer_management(In_shp, layerName)
# Make Table view from .xls
arcpy.MakeTableView_management(joinTable, join_tview)
# Join the feature layer to a table
arcpy.AddJoin_management(layerName, joinField1, join_tview, joinField2)
arcpy.CopyFeatures_management(layerName, output_shp)
except Exception as err:
print(err.args[0])
#add "update" field
lstFields = arcpy.ListFields(output_shp)
x = False
for field in lstFields:
if field.name == "update":
x = True
if x <> "True":
arcpy.AddField_management(output_shp, "updated", "STRING")
#calculate field
infield = "updated"
expression = 'myfunct(!AKSAS_Num_Txt!,!AKSAS_NUMBER!)'
codeblock = '''def myfunct(val1,val2):
if (val1 <> val2 or val1 == 'N/A' or val1 == '' or val2 == '' or val2 == 'N/A' or val1== None or val2 == None):
return "Not Updated"
else:
return "Updated"'''
arcpy.CalculateField_management(output_shp, infield, expression, "PYTHON_9.3", codeblock)
#calculate field
infield2 = "AKSAS_Num_Txt"
infield3 = "AKSAS_NUMBER"
infield4 = "STATUS_CATEGORY"
infield5 = "Project_Status"
expression3 = 'myfunct3(!AKSAS_Num_Txt!,!AKSAS_NUMBER!,!Project_Status!,!STATUS_CATEGORY!)'
codeblock3 = '''def myfunct3(inpt,inpt2,inpt3,inpt4):
if (inpt <> inpt2 or inpt == 'N/A' or inpt == '' or inpt2 == '' or inpt2 == 'N/A' or inpt== None or inpt2 == None):
result = inpt3
else:
result = inpt4
return result'''
arcpy.CalculateField_management(output_shp, infield5, expression3, "PYTHON_9.3", codeblock3)
#delete fields
arcpy.DeleteField_management(output_shp, ["STATUS_CATEGORY","AKSAS_NUMBER"])
##export drafthistoricprojectfootprints to excel
Input_Table = "DraftHistoricProjectFootprints.gdb/joined"
Output_Excel_File = "DraftHistoricProjectFootprints.xls"
arcpy.TableToExcel_conversion(Input_Table,Output_Excel_File)
我说这是有效的原因是因为“DraftHistoricProjectFootprints.xls”出现在正确的文件夹中。这部分代码之后的所有内容都没有:
##make csv sheets
os.system(r"\\dotatufs04\crm\Public\temp\DDahms\Python\Update_MRS\Scripts\run_macro_sort_construction.vbs")
wb = xlwt.Workbook(encoding="latin-1")
for filename in glob.glob(r"csv\*.csv"):
(f_path, f_name) = os.path.split(filename)
(f_short_name, f_extension) = os.path.splitext(f_name)
ws = wb.add_sheet(f_short_name)
spamReader = csv.reader(open(filename, 'rb'))
for rowx, row in enumerate(spamReader):
for colx, value in enumerate(row):
ws.write(rowx, colx, value)
svstr = 'Projects_In_MRS.xls'
wb.save(svstr)
os.system(r"\\dotatufs04\crm\Public\temp\DDahms\Python\Update_MRS\Scripts\run_macro_sort_drafthistoricprojectfootprints.vbs")
##delete sheet3 and historic projects sheet
os.remove("csv/historicprojectsDraftHistoricProjectFootprints.csv")
os.remove("csv/Sheet3.csv")
##read cvs and make lists from data
with open(r"csv\completed.csv", 'rb') as f:
reader = csv.reader(f)
completed_list = list(reader)
completed_list = [el[2] for el in completed_list]
with open(r"csv\construction.csv", 'rb') as f:
reader = csv.reader(f)
construction_list = list(reader)
construction_list = [el[2] for el in construction_list]
with open(r"csv\design.csv", 'rb') as f:
reader = csv.reader(f)
design_list = list(reader)
design_list = [el[2] for el in design_list]
with open(r"csv\proposed.csv", 'rb') as f:
reader = csv.reader(f)
proposed_list = list(reader)
proposed_list = [el[2] for el in proposed_list]
with open(r"csv\suspended.csv", 'rb') as f:
reader = csv.reader(f)
suspended_list = list(reader)
suspended_list = [el[2] for el in suspended_list]
with open(r"csv\historicprojectscompleted.csv", 'rb') as f:
reader = csv.reader(f)
historic_completed_list = list(reader)
historic_completed_list = [el[2] for el in historic_completed_list]
with open(r"csv\historicprojectsconstruction.csv", 'rb') as f:
reader = csv.reader(f)
historic_construction_list = list(reader)
historic_construction_list = [el[2] for el in historic_construction_list]
with open(r"csv\historicprojectsdesign.csv", 'rb') as f:
reader = csv.reader(f)
historic_design_list = list(reader)
historic_design_list = [el[2] for el in historic_design_list]
with open(r"csv\historicprojectsproposed.csv", 'rb') as f:
reader = csv.reader(f)
historic_proposed_list = list(reader)
historic_proposed_list = [el[2] for el in historic_proposed_list]
with open(r"csv\historicprojectssuspended.csv", 'rb') as f:
reader = csv.reader(f)
historic_suspended_list = list(reader)
historic_suspended_list = [el[2] for el in historic_suspended_list]
##compare lists and write to csv
compare_completed = list(set(completed_list)-set(historic_completed_list))
if not compare_completed:
compare_completed.append("All Projects are in Shapefile!")
with open(r"compared_lists\compare_completed.csv", "w") as output:
writer = csv.writer(output, lineterminator='\n')
for val in compare_completed:
writer.writerow([val])
compare_construction = list(set(construction_list)-set(historic_construction_list))
if not compare_construction:
compare_construction.append("All Projects are in Shapefile!")
with open(r"compared_lists\compare_construction.csv", "w") as output:
writer = csv.writer(output, lineterminator='\n')
for val in compare_construction:
writer.writerow([val])
compare_design = list(set(design_list)-set(historic_design_list))
if not compare_design:
compare_design.append("All Projects are in Shapefile!")
with open(r"compared_lists\compare_design.csv", "w") as output:
writer = csv.writer(output, lineterminator='\n')
for val in compare_design:
writer.writerow([val])
compare_proposed = list(set(proposed_list)-set(historic_proposed_list))
if not compare_proposed:
compare_proposed.append("All Projects are in Shapefile!")
with open(r"compared_lists\compare_proposed.csv", "w") as output:
writer = csv.writer(output, lineterminator='\n')
for val in compare_proposed:
writer.writerow([val])
compare_suspended = list(set(suspended_list)-set(historic_suspended_list))
if not compare_suspended:
compare_suspended.append("All Projects are in Shapefile!")
with open(r"compared_lists\compare_suspended.csv", "w") as output:
writer = csv.writer(output, lineterminator='\n')
for val in compare_suspended:
writer.writerow([val])
##combine csv to single workbook
wb = xlwt.Workbook()
for filename in glob.glob(r"compared_lists\*.csv"):
(f_path, f_name) = os.path.split(filename)
(f_short_name, f_extension) = os.path.splitext(f_name)
ws = wb.add_sheet(f_short_name)
spamReader = csv.reader(open(filename, 'rb'))
for rowx, row in enumerate(spamReader):
for colx, value in enumerate(row):
ws.write(rowx, colx, value)
wb.save(r"Projects_Missing_from_DraftHistoricProjectFootprints.xls")
##put proj names on worksheet
os.system(r"\\dotatufs04\crm\Public\temp\DDahms\Python\Update_MRS\Scripts\run_macro_get_proj_names_to_missing_proj.vbs")
##change project status to proper (sentence) case
expression99 = '!Project_Status!.title()'
arcpy.CalculateField_management(output_shp, "Project_Status", expression99, "PYTHON_9.3")
一旦这个脚本运行,它应该创建/填充一些不同的文档,如上所示,但相反,错误弹出并杀死它。如上所述,我不用Python编写代码,所以这对我来说都是新的。请解释你的解决方案,比如我五岁。
谢谢!