xml为SpreadSheetML格式(Excel的xml格式)。具体:https://blog.csdn.net/lmhuanying1012/article/details/78753851

经过删除<Style></Style>之后为

ps:一定要检查文件是否完整。解析错误可能是文件有问题。这个文件不完整,报错 parse unkown line 5443,因为没有</ss:Worksheet>之类。

1.  解析

import xml.dom.minidom
import pandas as pd


dom=xml.dom.minidom.parse('introduce.xml')
root=dom.documentElement#获得xml文档对象
use=root.getElementsByTagName('ss:Cell')#获得子标签

2.  xml转为dataframe

cols=['c_id','c_account','c_hope_time','c_hobby','c_introduce','c_choose_standard']
df_xml=pd.DataFrame(columns=cols)




#Row标签为最外层,取第一个cell标签,再取其中的Data标签,打印出来内容
i=0
c_id=0
while i<use.length:
   temp=use[i].getElementsByTagName('Data')[0].firstChild
#use[i]第几行数据,getElementsByTagName('nodeName')获得子标签,
#获得标签对之间的数据 temp.firstChild.data===temp.childNodes[0].nodevalue
   if temp:
       temp=temp.data
   else:
       temp='nan'#处理null
   if(i%6==0):
       c_id=temp
   elif i%6==1:
       c_account=temp
   elif i%6==2:
       c_hope_time=temp
   elif i%6==3:
       c_hobby=temp
   elif i%6==4:
       c_introduce=temp
   else:
       c_choose_standard=temp
     
   if (i%5==0) & (i>=10):
#dataframe添加记录df.append()
#pd.Series([数据],index=[列名])创建数组
       df_xml=df_xml.append(
           pd.Series([c_id,c_account,c_hope_time,c_hobby,c_introduce,c_choose_standard],
                      index=cols),ignore_index=True)      
   i=i+1 
   
   
#转为csv保存
df_xml['c_id']=df_xml['c_id'].astype('int')#某列类型转换
df_xml=df_xml.sort_values(by='c_id').reset_index(drop=True)#按照某列排序
df_xml=df_xml.drop_duplicates(subset=['c_id'])#pd去重
df_xml.to_csv('introduce.csv',index=False,encoding='utf_8_sig')#中文写入csv,注意乱码问题,encoding='utf_8_sig'