EOL 在计算字段上停止 python

发布于 2024-11-05 21:45:15 字数 1018 浏览 0 评论 0原文

有人可以帮助我修改这些脚本以忽略错误并继续运行吗?我只需要弄清楚如何使脚本跳过这些错误并完成其余的行。

这是完整的 Python 脚本:

# Import system modules
import sys, string, os, arcgisscripting

# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True

# Set the workspace. List all of the folders within
gp.Workspace = "C:\ZP4"
fcs = gp.ListWorkspaces("*","Folder")

for fc in fcs:
print fc
gp.CalculateField_management(fc + "\\Parcels.shp", "SIT_FULL_S", "myfunction(!SIT_HSE_NU!,!SIT_FULL_S!)", "PYTHON", "def myfunction(fld1,fld2):\n if (fld1=='0'or fld1=='00'or fld1<'00000000000'):\n   return ''\n else:\n   return fld2")

这是我遇到的错误: 回溯(最近一次调用最后一次):

  File "C:\Documents and Settings\Andrew\Desktop\HOUSENUMERZERO.py", line 18, in
 <module>

ERROR 000539: Error running expression: myfunction

(" ","69 FLOOD ST
") <type 'exceptions.SyntaxError'>: EOL while scanning single-quoted string (<st
ring>, line 1)

Failed to execute (CalculateField).

Would anyone be able to help me modify these scripts to ignore the error and continue running ? I just need to figure out how to make the script skip over these errors and finish the rest of the lines.

Here is the full Python script:

# Import system modules
import sys, string, os, arcgisscripting

# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True

# Set the workspace. List all of the folders within
gp.Workspace = "C:\ZP4"
fcs = gp.ListWorkspaces("*","Folder")

for fc in fcs:
print fc
gp.CalculateField_management(fc + "\\Parcels.shp", "SIT_FULL_S", "myfunction(!SIT_HSE_NU!,!SIT_FULL_S!)", "PYTHON", "def myfunction(fld1,fld2):\n if (fld1=='0'or fld1=='00'or fld1<'00000000000'):\n   return ''\n else:\n   return fld2")

And here is the error I encounter:
Traceback (most recent call last):

  File "C:\Documents and Settings\Andrew\Desktop\HOUSENUMERZERO.py", line 18, in
 <module>

ERROR 000539: Error running expression: myfunction

(" ","69 FLOOD ST
") <type 'exceptions.SyntaxError'>: EOL while scanning single-quoted string (<st
ring>, line 1)

Failed to execute (CalculateField).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为人所爱 2024-11-12 21:45:15

第一个选项:将 gp.CalculateField_management(...) 包装在 try/except 中,如下所示:

try:
    gp.CalculateField_management(...)
except SyntaxError:
    pass

这应该允许您的脚本继续运行,但我不确定 gp 将会。

更好的选择是预处理每个文件,并处理其中嵌入换行符的字段;像这样的东西:

for fc in fcs:
    fix_bad_fields(fp)
    gp.Calculatate...

并且 fix_bad_fields 看起来像(你必须研究这个,因为我不熟悉 .shp 文件 - 我会假装它允许写回同一个文件,但如果不是,你将不得不进行一些复制以及重命名):

def fix_bad_fields(filename):
    data_file = open_shp_file(filename)
    for row in data_file:
        row[0] = row[0].replace('\n', '')
        row[1] = row[1].replace('\n', '')
        row1.put_changes_on_disk() # force changes to disk (may not be necessary)
    data_file.close()

在这些细节上有很多猜测,但希望这能给你一个想法并足以继续下去。

First option: wrap the gp.CalculateField_management(...) in a try/except, like so:

try:
    gp.CalculateField_management(...)
except SyntaxError:
    pass

This should allow your script to keep going, but I'm not sure what the state of gp will be.

A better option would be to preprocess each file, and deal with the fields that have the embedd new-lines in them; something like:

for fc in fcs:
    fix_bad_fields(fp)
    gp.Calculatate...

and fix_bad_fields looks something like (you'll have to research this as I am unfamiliar with .shp files -- I'll pretend it allows writing back to the same file, but if not you'll have to do some copying and renaming as well):

def fix_bad_fields(filename):
    data_file = open_shp_file(filename)
    for row in data_file:
        row[0] = row[0].replace('\n', '')
        row[1] = row[1].replace('\n', '')
        row1.put_changes_on_disk() # force changes to disk (may not be necessary)
    data_file.close()

Lots of guesswork in those particulars, but hopefully that gives you an idea and enough to go on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文