我知道有人已经问过这个问题,但是我的问题是列表的大小。如果我的列表包含1000个以上的元素,如何创建包含这些元素的xml文件? 这是我要使用的代码:
import xml.etree.ElementTree as ET
import os
img_src_dir = input("Image folder: ")
video_id = img_src_dir[:-12]
img_list = os.listdir(img_src_dir)
for i in img_list:
# create the file structure
data = ET.Element('data')
images = ET.SubElement(data, 'images')
#HERE IS THE PROBLEM
#I want that every i is an item in the structure
item1 = ET.SubElement(images, 'image')
item2 = ET.SubElement(images, 'image')
.
.
.
item1.set('id',i[0])
item2.set('id',i[1])
.
.
.
# create a new XML file with the results
mydata = ET.tostring(data)
myfile = open(video_id + '.xml', "w")
myfile.write(mydata)