无法将xml文件转换为csv;相反,我得到了一个

2024-09-30 00:32:46 发布

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

我很难将一堆XML文件转换为csv的对象检测项目。我在跟踪这个链接 https://github.com/wagonhelm/TF_ObjectDetection_API/blob/master/ChessObjectDetection.ipynb

imagepath = os.path.join(r'C:\Users\snehal\Desktop\JPEGImages')
labelpath = os.path.join(r'C:\Users\snehal\Desktop\XMLAnnotations')


def xml_to_csv(path):
    xml_list=[]
    for xml_file in glob.glob(path + '/*.xml'):
        tree = ET.parse(xml_file)
        root = tree.getroot()
        for member in root.findall('object'):
            value = (root.find('filename').text,
                     int(root.find('size')[0].text),
                     int(root.find('size')[1].text),
                     member[0].text,
                     int(member[4][0].text),
                     int(member[4][1].text),
                     int(member[4][2].text),
                     int(member[4][3].text)
                     )
            xml_list.append(value)
        column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
        xml_df = pd.DataFrame(xml_list,columns=column_name)
        return xml_df



def main():

    for i in imagepath:
        path = i
        folder = os.path.basename(os.path.normpath(i))
        xml_df = xml_to_csv(path)
        xml_df.to_csv(path_or_buf=folder +'.csv.',index=None)
        print('Converted')

main()

预期的输出应该是一个文件夹中的许多csv文件,但我得到了一个错误:

runfile('C:/Users/snehal/Desktop/Ansh stuff/Object detection/TEST2.py', wdir='C:/Users/snehal/Desktop/Ansh stuff/Object detection')
Traceback (most recent call last):

  File "<ipython-input-40-caaa983ccae1>", line 1, in <module>
    runfile('C:/Users/snehal/Desktop/Ansh stuff/Object detection/TEST2.py', wdir='C:/Users/snehal/Desktop/Ansh stuff/Object detection')

  File "C:\Users\snehal\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 826, in runfile
    execfile(filename, namespace)

  File "C:\Users\snehal\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/snehal/Desktop/Ansh stuff/Object detection/TEST2.py", line 48, in <module>
    main()

  File "C:/Users/snehal/Desktop/Ansh stuff/Object detection/TEST2.py", line 45, in main
    xml_df.to_csv(path_or_buf=folder +'.csv.',index=None)

AttributeError: 'NoneType' object has no attribute 'to_csv'

我该怎么解决这个问题?你知道吗


Tags: csvpathtextinpyobjectxmlusers
1条回答
网友
1楼 · 发布于 2024-09-30 00:32:46

我认为这可能与folder = os.path.basename(os.path.normpath(i))这一行有关。i的值可能不是实际的文件路径,或者您可能没有写入它的权限。你知道吗

您可以打印出i的值来澄清。你知道吗

因此,当代码执行到xml_df.to_csv(path_or_buf=folder +'.csv.',index=None)时,folder的值是None,因此执行无法继续。你知道吗

相关问题 更多 >

    热门问题