Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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/0/xml/15.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
Python 如何解决索引器:单位置索引器超出范围?_Python_Xml_Pandas_Elementtree - Fatal编程技术网

Python 如何解决索引器:单位置索引器超出范围?

Python 如何解决索引器:单位置索引器超出范围?,python,xml,pandas,elementtree,Python,Xml,Pandas,Elementtree,我的预期输出如下: report_num|data 1 rr 1 r 1 a 但是,我对“Series”对象没有属性“value”,并且索引超出范围有问题。我还尝试将iloc[0]更改为loc[]或loc[:0]。但没有成功。希望有人能提供帮助 def report_num(xml_file,df_cols): global df xtree = et.parse(xml_file) xroot = xtree

我的预期输出如下:

report_num|data
1           rr
1           r
1           a
但是,我对“Series”对象没有属性“value”,并且索引超出范围有问题。我还尝试将iloc[0]更改为loc[]或loc[:0]。但没有成功。希望有人能提供帮助

def report_num(xml_file,df_cols):
    global df
    xtree = et.parse(xml_file)
    xroot = xtree.getroot()
    out_xml = pd.DataFrame(columns=df_cols)

    for node in xroot.findall('r:ReportHeader/r:Section[1]/r:Subreport/r:Details/r:Section[3]/r:Field',namespace):
        name = node.attrib.get('Name')
        value = node.find('r:Value',namespace).text
        out_xml = out_xml.append(
            pd.Series([value], index=df_cols), ignore_index=True)
    # df=out_xml.transpose-->if uncomment this will 'Series' object has no attribute 'value'

    return out_xml

def appendReportNum(path):
    report_num_df = rd.report_num(path, ['value'])
    report_number = report_num_df.iloc[0].value
    return report_number

def powTestC1():
    filelist = []
    pathh = r'C:/tester'
    for root, dirs, files in os.walk(pathh):
        for file in files:
            if file.endswith('.xml'):
                filelist.append(file)

        mainDf = pd.DataFrame()
        for filename in filelist:
            path = r'C:/tester/' + filename
            df = rd.pow_testparameter1(path, ['value'])
            report_num = appendReportNum(path)
            df.insert(0, "report_num", report_num)
            cond = con.condition_pow1(path, ['value'])
            rules = cond.iloc[0].value
            df.insert(1, "condition", rules)
            mainDf = pd.concat([mainDf, df])

        return mainDf

print(powTestC1())

最好是显示所有的工作,包括您正在使用的输入数据,以便其他人可以重新创建问题。但通常情况下,为什么会发生这种错误?因为只有一个文件使用iloc[0]时我工作得很好,所以我建议首先使用常规Python列表/dict,然后仅当您实际需要数据帧功能时才将它们组合到数据帧中……是的,但我认为是名称空间问题。bcoz我可以在删除名称空间后运行。