在python中,当试图获取特定值时抛出int'对象是不可iterable的?

2024-09-27 21:35:01 发布

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

我的python脚本中有下面的代码,我用它来获取JIRA问题的component字段的值,不管component/s字段有什么值,下面的逻辑是读取这些值,如果值与R Ciapara或CTX匹配,那么它将执行下一个任务,基本上删除文本部分,只获取数值从下面1.3和1.2英寸的弦开始案子。之后如果两个值相同,例如R Ciapara 1.3(已发布),R Ciapara 1.3,我将使其唯一。在这种情况下,它将给我1.3,1.3,那么我将使其为4.3

组件字段的示例为

Component/s: M Nx, M CitLx, R Ciapara 1.3 (Released), CTX 1.2

python脚本中的逻辑:

def Test(Host, issue):


  allModules = []                                      

  componentmatch = 0 
  for version in issue["fields"]["components"]:
    cacheData = json.dumps(version)
    jsonToPython = json.loads(cacheData)
    print jsonToPython
    #componentmatch = 0 
    if jsonToPython['name'][:10] == "R Ciapara " or version["name"][:4] == "CTX ":
      componentmatch += 1
      componentmatch = re.findall(r"\d+\.\d+", jsonToPython['name'])
    if componentmatch:
      allModules.append(componentmatch[0])
      d={i for i in allModules}
      p=list(d)

  matchcomp = ("[{}]".format(", ".join(p)))
  return matchcomp 

“int”对象不可iterable下面出现错误:

Processing TPT-3
R Ciapara 4.3 (Released)
Ciapara 1.4.90.0
{u'self': u'https://test/rest/api/2/component/730', u'id': u'730', u'name': u'M Nx'}
{u'self': u'https://test/rest/api/2/component/73', u'id': u'73', u'name': u'M CitLx'}
{u'self': u'https://test/rest/api/2/component/1', u'id': u'1', u'name': u'R Ciapara 1.3 (Released)'}
[1.3]
[1.4, 1.2]
Processing TPT-2
R Ciapara 1.3(U1)
Ciapara 1.4.3.4
{u'self': u'https://test/rest/api/2/component/7', u'id': u'7', u'name': u'R Ciapara 1.3(U1)'}
[1.3]
[1.4]
Processing TPT-1
R Ciapara 1.4
Ciapara 1.4.73.4
{u'self': u'https://test/rest/api/2/component/733', u'id': u'733', u'name': u'R Ciapara 1.4'}
{u'self': u'https://test/rest/api/2/component/7335', u'id': u'7335', u'name': u'R Ciapara 1.4(U1)'}
Traceback (most recent call last):
TypeError: 'int' object is not iterable

无法理解我在这里错过了什么?你知道吗


Tags: namehttpstestselfrestapiidversion
2条回答

您正在将1添加到componentmatch此处:

componentmatch += 1

然后将componentmatch重新分配给re.findall()的返回输出,它在这里返回字符串列表:

componentmatch = re.findall(r"\d+\.\d+", jsonToPython['name'])

然后再次输入if语句:

if jsonToPython['name'][:10] == "R Ciapara " or version["name"][:4] == "CTX ":

现在您正试图再次将1添加到字符串列表中,这将引发以下错误:

componentmatch += 1 TypeError: 'int' object is not iterable

您正试图向字符串列表中添加整数。你知道吗

现在,我改变了我的逻辑,它的工作

  allModules = []                                      

  Fixversionmatch = 0  
  for version in issue["fields"]["fixVersions"]:
    if version['name'][:8] == "Ciapara " or version["name"][:4] == "CTX ":  
      cacheData = json.dumps(version)
      jsonToPython = json.loads(cacheData)    
      Fixversionmatch = re.findall(r"(\d+\.\d+)\.\d+\.\d+", jsonToPython['name'])
    if Fixversionmatch:
      allModules.append(Fixversionmatch[0])
      d={i for i in allModules}
      p=list(d)


  matchFix = ("[{}]".format(", ".join(p)))
  return matchFix

相关问题 更多 >

    热门问题