-5

我有一个问题,函数返回一个数字。然后,当我尝试组装一个包含该数字的 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),它吐出一个数字,但我不再得到 URL 的其余部分......

想法?帮助?

好的...为了显示我的 baseURL 和我的 configurationID 是我所做的。

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
4

1 回答 1

3

configurationID的是None。这可能意味着generate_configurationID()没有返回值。Python 中没有办法让变量名“丢失”它的值。在您发布的代码中,唯一的方法是configurationID返回,如果您没有明确返回任何值,就会发生这种情况。Nonegenerate_configurationID()None

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

您可能会通过完整发布来证明我错了generate_configurationID(),我承认您的程序很神奇

于 2012-04-23T20:35:47.563 回答