TypeError:在Python中

时间:2012-04-23 19:44:24

标签: python

我有一个问题,一个函数返回一个数字。当我尝试汇编包含该号码的URL时,我遇到了失败。

特别是我得到的错误是

  

TypeError:无法连接'str'和'NoneType'对象

不知道从哪里开始。

以下是相关的代码:

# Get the raw ID number of the current configuration
configurationID = generate_configurationID()

# Update config name at in Cloud
updateConfigLog = open(logBase+'change_config_name_log.xml', 'w')
# Redirect stdout to file
sys.stdout = updateConfigLog
rest.rest(('put', baseURL+'configurations/'+configurationID+'?name=this_is_a_test_', user, token))
sys.stdout = sys.__stdout__

如果我在rest.rest()

中手动输入以下内容,则效果很好
rest.rest(('put', http://myurl.com/configurations/123456?name=this_is_a_test_, myusername, mypassword))

我已经尝试了str(configurationID)并且它会回吐一个数字,但我不再获得其余的网址...

想法?帮助

好的......试图在这里展示我的baseURL和我的配置ID就是我做的。

print 'baseURL: '+baseURL
print 'configurationID: '+configurationID

这就是我回来的原因

it-tone:trunk USER$ ./skynet.py fresh
baseURL: https://myurl.com/
369596
Traceback (most recent call last):
  File "./skynet.py", line 173, in <module>
    main()
  File "./skynet.py", line 30, in main
    fresh()
  File "./skynet.py", line 162, in fresh
    updateConfiguration()
  File "./skynet.py", line 78, in updateConfiguration
    print 'configurationID: '+configurationID
TypeError: cannot concatenate 'str' and 'NoneType' objects
it-tone:trunk USER$ 

对我来说有趣的是369596是配置ID,但就像之前它似乎破坏了围绕它的所有内容。

如下所述,我的generate_configurationID不是返回值,而是打印它。

# from generate_configurationID
def generate_configurationID():
  dom = parse(logBase+'provision_template_log.xml')
  name = dom.getElementsByTagName('id')
  p = name[0].firstChild.nodeValue
  print p
  return p

1 个答案:

答案 0 :(得分:3)

您的configurationIDNone。这可能意味着generate_configurationID()没有返回值。在Python中,变量名称无法“丢失”其值。在您发布的代码中,configurationIDNone的唯一方法是generate_configurationID()返回None,如果您未明确返回任何内容,将会发生这种情况值。

“但是它会在屏幕上打印configurationID!”你可能反对。当然,但这可能在generate_configurationID(),你打印它以确保它是正确的但是忘记归还它。

您可以通过完整发布generate_configurationID()来证明我的错误,我承认您的计划魔力