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

时间:2019-11-14 02:36:04

标签: python xml pandas elementtree

我的预期输出如下:

report_num|data
1           rr
1           r
1           a

但是,我对“系列”对象没有属性“值”并且索引超出范围存在疑问。我也尝试将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())

0 个答案:

没有答案