类型错误:“int”当我尝试迭代内部循环时,对象不可迭代?

发布于 2024-11-15 13:26:35 字数 2096 浏览 0 评论 0原文

我不确定我的循环结构有什么问题。也许这是一个命名问题,因为计数变量和子元素名称使用相同的变量。任何帮助表示赞赏。

from xml.etree import ElementTree as ET

root = ET.Element("painter")
root.set('version', '1.0')
linenum = 0
pointnum = 0

smpl_data = [[[20,40],(0,0,1,1)],[[10,50],(0,0,1,1)],[[78,89],(0,0,1,1)]]

while linenum <= len(smpl_data): #smpl_data change to self.lines
    elem_line = ET.SubElement(root,"line" + str(linenum), attrib={"r": "1", "g": "2", "b": "3", "a": "4"})                     
    elem_line.set("r", smpl_data[linenum][1][0])
    print elem_line.attrib.get("r")                      
    elem_line.set("g", smpl_data[linenum][1][1])
    print elem_line.attrib.get("g")
    elem_line.set("b", smpl_data[linenum][1][2])
    print elem_line.attrib.get("b")
    print elem_line.get("a")
    elem_line.set("a", smpl_data[linenum][1][3])
    print elem_line.attrib.get("a")

    for pointnum in linenum:
        elem_point = ET.SubElement("line" + str(linenum), "point" + str(pointnum), attrib={x: "10", y: "20"})
        print elem_point
        print elem_point.get("x")
        elem_point.set("x", smpl_data[linenum][0][0])
        print elem_point.attrib.get("x")
        print elem_point.get("y")
        elem_point.set("y", smpl_data[linenum][0][1])
        print elem_point.attrib.get("y")
        pointnum = pointnum + 1
   linenum = linenum + 1

当它尝试开始迭代内部循环时,我得到了错误,for pointnum in linenum。不知道为什么?

我很抱歉没有说得更清楚。

这是完整的错误:

  Traceback (most recent call last):
  File "C:\Users\joel\Desktop\open-autism-software\software\asd\pen\writexml.py", line 57, in <module>
  for pointnum in linenum:
  TypeError: 'int' object is not iterable

我在完整程序中的目标是能够将所有特定的 (x,y) 点作为属性添加到它们各自的子元素(线)。每根线都附着在主根上。但是,(x,y)点的数量随着我的持久数据而变化,因为每条线的长度都可以不同。

XML 文档应如下所示:

<root>
<line r="0", g="0", b="1", a="1">
    <point x="20" y="30">
    <point x="10" y="15">
    <point x="15" y="25">
    ...
</line>
<line r="0", g"1", b="1", a="1">
    ...
</line>
...
</root>

I am not sure what the problem is with my loop structure. Maybe it's a naming problem since the count variable and SubElement names use the same variable. Any help is appreciated.

from xml.etree import ElementTree as ET

root = ET.Element("painter")
root.set('version', '1.0')
linenum = 0
pointnum = 0

smpl_data = [[[20,40],(0,0,1,1)],[[10,50],(0,0,1,1)],[[78,89],(0,0,1,1)]]

while linenum <= len(smpl_data): #smpl_data change to self.lines
    elem_line = ET.SubElement(root,"line" + str(linenum), attrib={"r": "1", "g": "2", "b": "3", "a": "4"})                     
    elem_line.set("r", smpl_data[linenum][1][0])
    print elem_line.attrib.get("r")                      
    elem_line.set("g", smpl_data[linenum][1][1])
    print elem_line.attrib.get("g")
    elem_line.set("b", smpl_data[linenum][1][2])
    print elem_line.attrib.get("b")
    print elem_line.get("a")
    elem_line.set("a", smpl_data[linenum][1][3])
    print elem_line.attrib.get("a")

    for pointnum in linenum:
        elem_point = ET.SubElement("line" + str(linenum), "point" + str(pointnum), attrib={x: "10", y: "20"})
        print elem_point
        print elem_point.get("x")
        elem_point.set("x", smpl_data[linenum][0][0])
        print elem_point.attrib.get("x")
        print elem_point.get("y")
        elem_point.set("y", smpl_data[linenum][0][1])
        print elem_point.attrib.get("y")
        pointnum = pointnum + 1
   linenum = linenum + 1

I get the error right when it tries to begin iterating through the inner loop, for pointnum in linenum. Not sure why?

I apologize for not being more clear.

This is the full error:

  Traceback (most recent call last):
  File "C:\Users\joel\Desktop\open-autism-software\software\asd\pen\writexml.py", line 57, in <module>
  for pointnum in linenum:
  TypeError: 'int' object is not iterable

My goal in the full program is to be able to add all the specific (x,y) points as attributes to their respective SubElement (line). Each line is attached to the main root. However, the number of (x,y) points is variable with my persistent data because each line can be a different length.

The XML doc should look something like this:

<root>
<line r="0", g="0", b="1", a="1">
    <point x="20" y="30">
    <point x="10" y="15">
    <point x="15" y="25">
    ...
</line>
<line r="0", g"1", b="1", a="1">
    ...
</line>
...
</root>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃繁缕 2024-11-22 13:26:35

你可能想要的

for pointnum in range(linenum):

是,所有从零到一小于 linenum 的数字

You probably want

for pointnum in range(linenum):

That is, all the numbers from zero to one less than linenum

街角卖回忆 2024-11-22 13:26:35

linenum 不可迭代。很难说你在这里想做什么,但也许 for pointnum in smpl_data[linenum]: 就是你想要的?

linenum is not iterable. It's hard to tell what you are trying to do here, but perhaps for pointnum in smpl_data[linenum]: would be what you want?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文