比较字符串会导致Runtim

2024-10-03 04:28:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我比较了在脚本中作为字符串存储的两个guid,这会导致运行时错误。如果我更改If语句,它会将运行时错误推送到父for循环。我可以在IF语句上方添加一个显然无用的for循环,然后它似乎可以修复运行时错误。你知道吗

以下是遇到错误的初始代码:

for row in cursor:
    print 'Remote len: '+str(len(agoFeatures))
    for agoFeature in agoFeatures:
        print 'row[2]: ', row[2], type(row[2]), len(row[2])
        print "agoFeature[a][s]: ", \
              agoFeatures['attributes']['SegmentID'].upper(), \
              type(agoFeature['attributes']['SegmentID'].upper()), \
              len(agoFeature['attributes']['SegmentID'].upper())
        localFeature = row[2]
        remoteFeature = '{'+agoFeature['attributes']['SegmentID'].upper()+'}'

        print 'localFeature: ', localFeature, type(localFeature), len(localFeature)
        print 'remoteFeature: ', remoteFeature, type(remoteFeature), len(remoteFeature)

        if localFeature == remoteFeature:
            print 'Local == Remote'

运行时,它将返回以下内容:

Remote len: 2
row[2]:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
agoFeature[a][s]:  ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD <type 'unicode'> 36
localFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
remoteFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
Local == Remote
row[2]:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
agoFeature[a][s]:  714215DE-7E54-4E3E-8078-3C90E5407237 <type 'unicode'> 36
localFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
remoteFeature:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
Remote len: 2
row[2]:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
agoFeature[a][s]:  ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD <type 'unicode'> 36
localFeature:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
remoteFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
Traceback (most recent call last):
  File "GetUpdates.py", line 262, in <module>
    main()
  File "GetUpdates.py", line 248, in main
    compareAGOFeaturesToLocal()
  File "GetUpdates.py", line 147, in compareAGOFeaturesToLocal
    if localFeature == remoteFeature:
RuntimeError

这将比较两个guid,并在第三次比较(7142对ED24)后崩溃IF语句。如果我将If语句改为以下内容,它将一直持续到结束(7142对7142),但会在for循环中崩溃。你知道吗

if str(localFeature) == str(remoteFeature):
    print 'Local == Remote'

结果:

Remote len: 2
row[2]:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
agoFeature[a][s]:  ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD <type 'unicode'> 36
localFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
remoteFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
Local == Remote
row[2]:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
agoFeature[a][s]:  714215DE-7E54-4E3E-8078-3C90E5407237 <type 'unicode'> 36
localFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
remoteFeature:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
Remote len: 2
row[2]:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
agoFeature[a][s]:  ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD <type 'unicode'> 36
localFeature:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
remoteFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
row[2]:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
agoFeature[a][s]:  714215DE-7E54-4E3E-8078-3C90E5407237 <type 'unicode'> 36
localFeature:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
remoteFeature:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
Local == Remote
Traceback (most recent call last):
  File "GetUpdates.py", line 262, in <module>
    main()
  File "GetUpdates.py", line 248, in main
    compareAGOFeaturesToLocal()
  File "GetUpdates.py", line 137, in compareAGOFeaturesToLocal
    for agoFeature in agoFeatures:
RuntimeError

我以为文本中有什么东西引起了问题。我从IF语句中删除了str,并添加了for循环来打印remoteFeature中的每个字符。你知道吗

for row in cursor:
    print 'Remote len: '+str(len(agoFeatures))
    for agoFeature in agoFeatures:
        print 'row[2]: ', row[2], type(row[2]), len(row[2])
        print "agoFeature[a][s]: ", \
              agoFeatures['attributes']['SegmentID'].upper(), \
              type(agoFeature['attributes']['SegmentID'].upper()), \
              len(agoFeature['attributes']['SegmentID'].upper())
        localFeature = row[2]
        remoteFeature = '{'+agoFeature['attributes']['SegmentID'].upper()+'}'

        print 'localFeature: ', localFeature, type(localFeature), len(localFeature)
        print 'remoteFeature: ', remoteFeature, type(remoteFeature), len(remoteFeature)

        for i, char in enumerate(remoteFeature):
                i, ord(char), char

        if localFeature == remoteFeature:
            print 'Local == Remote'

我删除了print语句,因为它不打印任何有用的内容(只显示每个期望的字符)。添加的for循环不起任何作用,但允许它成功完成:

Remote len: 2
row[2]:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
agoFeature[a][s]:  ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD <type 'unicode'> 36
localFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
remoteFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
Local == Remote
row[2]:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
agoFeature[a][s]:  714215DE-7E54-4E3E-8078-3C90E5407237 <type 'unicode'> 36
localFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
remoteFeature:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
Remote len: 2
row[2]:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
agoFeature[a][s]:  ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD <type 'unicode'> 36
localFeature:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
remoteFeature:  {ED24C58B-253F-4D8A-8C1E-EBFFC57B4FDD} <type 'unicode'> 38
row[2]:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
agoFeature[a][s]:  714215DE-7E54-4E3E-8078-3C90E5407237 <type 'unicode'> 36
localFeature:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
remoteFeature:  {714215DE-7E54-4E3E-8078-3C90E5407237} <type 'unicode'> 38
Local == Remote
Start: 2014-08-14 11:41:20.628000
End: 2014-08-14 11:41:24.685000
Difference: 0:00:04.057000

打印开始/结束/差异在脚本末尾处理。你知道为什么会发生这样的错误吗?或者我该怎么处理?我不喜欢额外的for循环,因为这是一个黑客。你知道吗


Tags: inforlenremotetypeunicodeattributesrow
1条回答
网友
1楼 · 发布于 2024-10-03 04:28:24

原始for循环中的cursor对象是使用arcpy从SearchCursor创建的(ArcGIS 10.2.2python库:http://resources.arcgis.com/en/help/main/10.2/index.html#//000v00000001000000)。你知道吗

with arcpy.da.SearchCursor(localMain, ['Main_ID', 'OBJECTID', 'SegmentID', 'Shape@JSON'], where) as cursor:

从光标打印行时,得到以下值:

(None, 1007, u'{714215DE-7E54-4E3E-8078-3C90E5407237}', <nil>)

那个形状@JSON返回引起错误的<nil>。虽然我没有与元组中的nil值交互,但这是导致问题的原因。如果我确实通过打印第[3]行来与它交互,它将导致python崩溃。你知道吗

问题是来自第三方软件包的坏数据。你知道吗

相关问题 更多 >