import xml.etree.ElementTree as ET
import os
import pandas as pd
from tqdm import tqdm
# folder_path = "E:/"  # 替换为你的文件夹路径
#
# # 获取文件夹中的所有文件
# file_list = os.listdir(folder_path)
tree = ET.parse('book.xml')
#我们使用tree.getroot()将解析器中的根元素提取出来，并将其存储在命名为“root”的变量中。
root = tree.getroot()

#我们创建了一个空的DataFrame，包含四个列：id、title、author和price。
df = pd.DataFrame(columns=['id', 'title', 'author', 'price'])
# for file_name in file_list:
#     if file_name.endswith(".xml"):
#         file_path = os.path.join(folder_path, file_name)
#
#         # 解析XML文件
#         tree = ET.parse(file_path)
#         root = tree.getroot()


for book in root:
    if book.tag=='book':
        id = book.find('id').text
        title = book.find('title').text
        author = book.find('author').text
        price = book.find('price').text
        devices=book.find('devices')
        tracks=devices.find('tracks')
        print(devices)
        print(tracks)
        track=tracks.find('track')
        tarRng=track.find('tarRng').text
        df = df.append(pd.Series([id, title, author, price,tarRng], index=['id', 'title', 'author', 'price','tarRng']), ignore_index=True)

# for book in root.findall("book"):
#     id = book.find('id').text
#     title = book.find('title').text
#     author = book.find('author').text
#     price = book.find('price').text
#     df = df.append(pd.Series([id, title, author, price], index=['id', 'title', 'author', 'price']), ignore_index=True)

# df.to_excel('books.xlsx', index=False)
filename="default - 20230601-133944.replay"
filename=filename.split(".")[0]
print(filename)