迭代一个包含8526个项目的列表,使索引超出范围

2024-07-02 10:30:54 发布

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

我是Python新手。我一直在为一个工作中的项目编写一个将XML转换为CSV的小脚本。XML数据中有406个元素被解析到内存中,每个元素有21个indexfields,它们是该元素的子元素,总共有8526个indexfields。你知道吗

在给定元素的这21个索引字段中,我想提取第0、第1、第2、第3、第5、第6、第7和第8项。你知道吗

我创建了一个循环(类似于下面的代码)来执行以下操作:

i = 0
files = 406
docrange = 8526
iterstep = 21
pad = '","'
for docs in range(i, docrange):
    string1 = str('"'+indexfields[iterstep])+pad)
    string2 = str(indexfields[iterstep+1])+pad)
    string3 = str(indexfields[iterstep+2])+pad)
    string5 = str(indexfields[iterstep+3])+pad)
    string6 = str(indexfields[iterstep+5])+pad)
    string7 = str(indexfields[iterstep+6])+pad)
    string8 = str(indexfields[iterstep+7])+pad)
    string9 = str(indexfields[iterstep+8])+pad)
    strung = string1+string2+string3+string5+string6+string7+string8+string9
    print strung

    iterstep = (iterstep + 21)
    i = (i + 1)

当我进入这个循环时,我得到一个错误:

Traceback (most recent call last): 
    File "Path/To/My/script.py", line 55, in <module> 
string1 = (str(indexfields[iterstep])) IndexError: list index out of range

从我收集的信息来看,我并不认为我在修改我正在迭代的列表,因为堆栈溢出上处理这个错误的其他线程似乎表明了这一点。你知道吗

虽然我知道有更优雅的方法来编写上面的代码,但我想快速将XML数据转换为CSV,并需要完成这项工作。你知道吗

Edit1:这可能不是合适的地方,但这里有一个例子文件.xml正在从中提取索引信息。另一个XML文件正在与一起使用文件.xml,如果需要,我也可以发布。你知道吗

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<File>
  <ExtendedProperties />
  <ID>7514</ID>
  <SyncFlag>Edit</SyncFlag>
  <CustomSort />
  <ViewerContext>None</ViewerContext>
  <ProjectID>1</ProjectID>
  <BoxID>0</BoxID>
  <FileID>7514</FileID>
  <FilePtr>0</FilePtr>
  <Status>Active</Status>
  <KeyValue />
  <KeyVisualValue />
  <Field>
    <string>Some words that I would like to pull1</string>
    <string>Some words that I would like to pull2</string>
    <string>Some words that I would like to pull3</string>
    <string>Some words that I would like to pull4</string>
    <string>Nonsense</string>
    <string>Some words that I would like to pull5</string>
    <string>Some words that I would like to pull6</string>
    <string>Some words that I would like to pull7</string>
    <string>Some words that I would like to pull8</string>
    <string>Some words that I would like to pull9</string>
    <string>Nonsense</string>
    <string>Nonsense</string>
    <string />
    <string />
    <string />
    <string />
    <string />
    <string />
    <string />
    <string />
    <string />
  </Field>
  <Notes />
  <DateStarted>2015-07-16T11:02:00</DateStarted>
  <DateChanged>2015-12-09T14:46:58.7335221-05:00</DateChanged>
  <ChangedBy>1</ChangedBy>
  <Destruction>1990-01-01T01:00:00</Destruction>
  <LabelPrinted>1990-01-01T01:00:00</LabelPrinted>
  <SaveStyle>NewFile</SaveStyle>
  <SaveNotesOnly>false</SaveNotesOnly>
  <FileVerifyLevels>0</FileVerifyLevels>
  <RemoteID>1</RemoteID>
</File>

这是一个来自文档.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Document>
  <ID>262601</ID>
  <SyncFlag>Edit</SyncFlag>
  <CustomSort />
  <ViewerContext>None</ViewerContext>
  <FileID>1647</FileID>
  <RelProjectID>0</RelProjectID>
  <ArchiveID>0</ArchiveID>
  <Archive />
  <DocumentID>262601</DocumentID>
  <Status>Active</Status>
  <Separator>Some words to pull</Separator>
  <Divider>Some words to pull</Divider>
</Document>

编辑2: 你们都给出了一些很好的回答,我发现我最初错误的原因是迭代值最初被设置为21而不是0。 我需要更多的指导这个软件,它会更好地打开一个新的线程?你知道吗


Tags: 文件to元素stringthatsomexmllike
3条回答

我相信你的问题是你说你有406行,每行21个元素,总共8562个元素。这意味着您需要遍历行数。如果您将iterstep增加218562倍,那么iterstep将等于179802,这是您拥有的元素数的21倍。所以您需要将范围更改为range(i,files)。您还可以从21开始iterstep,而您可能需要从零开始

files = 406
docrange = 8526
iterstep = 0
pad = '","'
for i in range(0, files):
    string1 = str('"'+indexfields[iterstep])+pad)
    string2 = str(indexfields[iterstep+1])+pad)
    string3 = str(indexfields[iterstep+2])+pad)
    string5 = str(indexfields[iterstep+3])+pad)
    string6 = str(indexfields[iterstep+5])+pad)
    string7 = str(indexfields[iterstep+6])+pad)
    string8 = str(indexfields[iterstep+7])+pad)
    string9 = str(indexfields[iterstep+8])+pad)
    strung = string1+string2+string3+string5+string6+string7+string8+string9
    print strung

    iterstep += 21

或者更好的方法是删除iterstep,每次只使用xrangesstep参数和step21

files = 406
docrange = 8526
pad = '","'
for i in range(0, docrange, 21):
    string1 = str('"'+indexfields[i])+pad)
    string2 = str(indexfields[i+1])+pad)
    string3 = str(indexfields[i+2])+pad)
    string5 = str(indexfields[i+3])+pad)
    string6 = str(indexfields[i+5])+pad)
    string7 = str(indexfields[i+6])+pad)
    string8 = str(indexfields[i+7])+pad)
    string9 = str(indexfields[i+8])+pad)
    strung = string1+string2+string3+string5+string6+string7+string8+string9
    print strung

编辑:您在这里的主要重点应该是学习如何读取堆栈跟踪。这告诉您的是,在某个循环之后,您正在尝试访问一个尚未填充的索引。试着一边打印索引,看看会发生什么。你知道吗

对于这方面的未来工作,我建议使用XML解析来完成这项工作。你知道吗

您应该动态地处理这个转换,而不是总是假设有x个元素。你知道吗

Python内置了解析XML和使用Xpath的模块。https://docs.python.org/2/library/xml.etree.elementtree.html

这使您能够解析单个节点、访问属性等

这个问题当然是由iterstep引起的,您从21开始,每次迭代都将其递增21。也许您应该将它保持在0,并且必须对i做些什么(因为在循环中更改它不会影响range),或者完全删除它。你知道吗

相关问题 更多 >