字符串替换不能按预期工作

时间:2014-05-08 12:26:31

标签: python ogr

我使用python来读取shapefile,但是我遇到了一些问题,这是核心代码:

value="#code"
print 'value:',value
if '#' in value:
    v=value.replace('#','');
    print 'replaced:',v
    fixed_value=str(feature.GetFieldAsString(v))

如果我直接运行脚本,它会按预期工作,但是一旦我在Web环境中运行它就会抛出错误,我会得到这样的错误:

  File "/home/kk/gis/codes/tilestache/map/__init__.py", line 162, in _get_features
    fixed_value=str(feature.GetFieldAsString(v))
  File "/usr/local/lib/python2.7/dist-packages/GDAL-1.10.1-py2.7-linux-x86_64.egg/osgeo/ogr.py", line 2233, in GetFieldAsString
    return _ogr.Feature_GetFieldAsString(self, *args)
NotImplementedError: Wrong number of arguments for overloaded function 'Feature_GetFieldAsString'.

如果我将该行更改为:

fixed_value=str(feature.GetFieldAsString('code'))

有效。

发生了什么?

似乎python中的replace函数使事情变得奇怪。


更新

似乎我明白了,这是由python中的replace函数引起的,它返回的是另一个而不是str

value='#code'
type(value) ==> str
v=value.replace('#','')
type(v) ==>unicode

然后我用:

 fixed_value=str(feature.GetFieldAsString(str(v)))

有效。

但我不确定它为什么在shell环境中工作,但在Web环境中却不行。我希望有人可以解释一下。

0 个答案:

没有答案
相关问题