Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用带有len()的range函数在python中从xml文件打印行和列_Python_Range - Fatal编程技术网

使用带有len()的range函数在python中从xml文件打印行和列

使用带有len()的range函数在python中从xml文件打印行和列,python,range,Python,Range,我有1000多行是通过python中的xml文件导入的,我想从第一行开始,每第二行从三个选定的选项卡/列打印到第20行,但我不知道如何将range函数放入xml文件的print函数中。请参见下面的示例代码: ##使用元素树导入xml文件 import xml.etree.ElementTree as ET xmlFile = "FilePath/DataName.xml" tree = ET.parse(xmlFile) root = tree.getroot() try

我有1000多行是通过python中的xml文件导入的,我想从第一行开始,每第二行从三个选定的选项卡/列打印到第20行,但我不知道如何将range函数放入xml文件的print函数中。请参见下面的示例代码:

##使用元素树导入xml文件

import xml.etree.ElementTree as ET

xmlFile = "FilePath/DataName.xml"
tree = ET.parse(xmlFile)
root = tree.getroot()

try: import xml.etree.ElementTree as ET
except ImportError:
    print('An error occurred trying to read the file.')

print(root[0].tag)
##示例1##这将打印所选标记/列的所有内容###

##例2##我可以每隔一行打印一次,但不能输入最大值。如果我输入(0,19,2)作为len()的范围,我会得到一个错误

print(root[0].tag)

for x in root.findall('root'):
    tag1 =x.find('tag1').text
    tag4 = x.find('tag4').text
    tag5 = x.find('tag5').text
    selectedrows = [tag1, tag4, tag5]
    for i in range(0, len(selectedrows), 2):
        print(selectedrows[i])

##上面的代码打印选定的列/标记,但如何将范围(0、19、2)包含到上面的范围函数中。我已经阅读了许多使用范围(0,len(??)函数在线打印文本的教程,但没有一个详细介绍如何在打印时选择行和列。甚至有可能做到这一点吗?或者我应该另存为CSV并创建数组吗?

在您的代码
中,selectedrows
仅为x循环的每次迭代保存三个标记。如果我理解正确,您希望在1 for x
循环之前初始化列表
selectedrows
,将三个标记作为此列表的一个元素附加,并在解析xml后,打印
selectedrows`的子集。像这样的

selectedrows = []
for x in root.findall('root'):
    tag1 =x.find('tag1').text
    tag4 = x.find('tag4').text
    tag5 = x.find('tag5').text
    selectedrows.append([tag1, tag4, tag5])

# this will run after you have gone through your xml doc with selectedrows populated
for i in range(0, len(selectedrows), 2):
    print(selectedrows[i])

上述内容将每隔一行打印一次(不在第20行停止)。如果需要,可以将范围(0、19、2)中的i修改为
。另外,如果您只需要前20行,那么您可能需要在20次迭代后停止x的
循环

太棒了,谢谢Piterberg。所以我错的地方是我试图用范围函数和打印函数。这很有效,我可以打印前20行中的每第2行。非常感谢你在这方面的指导,非常感谢。
selectedrows = []
for x in root.findall('root'):
    tag1 =x.find('tag1').text
    tag4 = x.find('tag4').text
    tag5 = x.find('tag5').text
    selectedrows.append([tag1, tag4, tag5])

# this will run after you have gone through your xml doc with selectedrows populated
for i in range(0, len(selectedrows), 2):
    print(selectedrows[i])