比较字符串会导致运行时错误

时间:2014-08-14 16:51:16

标签: 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,并在第三次比较后对IF语句进行了崩溃(7142对ED24)。如果我将IF语句更改为以下内容,它将一直持续到结束(7142 vs 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循环没有做任何事情,但DOES允许它成功完成:

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循环,因为它是如此黑客。

1 个答案:

答案 0 :(得分:0)

原始for循环中的光标对象是使用arcpy从SearchCursor创建的(ArcGIS 10.2.2 python库: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>)

Shape @ JSON返回<nil>,导致错误。虽然我没有与元组中的nil值进行交互,但它导致了问题。如果我通过尝试打印行[3]与它进行交互,则会导致python崩溃。

问题是来自第三方软件包的错误数据。