【问题标题】:Parsing Excel XML into R将 Excel XML 解析为 R
【发布时间】:2019-09-25 15:40:07
【问题描述】:

嗨,我知道有很多包可以做到这一点,但没有一个包可以处理我的这个特定文件。

我需要将此文件从 XML 解析为数据框。它应该忽略所有格式,只给我我的数据。

我尝试过 XML、XML2、OPENXLSX、TIDYXL、READR 和 XLSX 包,但没有一个可以做到。他们要么说这不是 XLS/XLXS 文件,要么是一堆乱七八糟的数据

library(XML)
fileurl <- "pep_slim.xml"
doc <- xmlParse(fileurl,useInternalNodes = TRUE) 
doc
xL <- xmlToList(doc)
data <- ldply(xL, data.frame)
head(data)

完整文件在下面并以 *.xml 结尾

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Author>Proctor, Stephon N</Author>
  <LastAuthor>Stephon Proctor</LastAuthor>
  <Created>2019-09-25T13:30:11Z</Created>
  <Version>16.00</Version>
 </DocumentProperties>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>10590</WindowHeight>
  <WindowWidth>25440</WindowWidth>
  <WindowTopX>0</WindowTopX>
  <WindowTopY>0</WindowTopY>
  <RefModeR1C1/>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s62">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Arial" ss:Color="#FFFFFF" ss:Bold="1"/>
   <Interior ss:Color="#4F81BD" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s63">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Arial" ss:Color="#FFFFFF" ss:Bold="1"/>
   <Interior ss:Color="#4FBD81" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s64">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Arial" ss:Color="#FFFFFF" ss:Bold="1"/>
   <Interior ss:Color="#A081BD" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s65">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Arial" ss:Color="#FFFFFF" ss:Bold="1"/>
   <Interior ss:Color="#D85647" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s66">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Arial" ss:Color="#FFFFFF" ss:Bold="1"/>
   <Interior ss:Color="#FF9900" ss:Pattern="Solid"/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s67">
   <Alignment ss:Horizontal="Left" ss:Vertical="Top" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"
     ss:Color="#020202"/>
   </Borders>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
 </Styles>
 <Worksheet ss:Name="MetricData">
  <Names>
   <NamedRange ss:Name="_FilterDatabase" ss:RefersTo="=MetricData!R1C1:R1C15"
    ss:Hidden="1"/>
  </Names>
  <Table ss:ExpandedColumnCount="15" ss:ExpandedRowCount="2" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
   <Column ss:AutoFitWidth="0" ss:Width="80.25" ss:Span="2"/>
   <Column ss:Index="4" ss:AutoFitWidth="0" ss:Width="159.75"/>
   <Column ss:AutoFitWidth="0" ss:Width="90" ss:Span="3"/>
   <Column ss:Index="9" ss:AutoFitWidth="0" ss:Width="140.25" ss:Span="1"/>
   <Column ss:Index="11" ss:AutoFitWidth="0" ss:Width="159.75"/>
   <Column ss:AutoFitWidth="0" ss:Width="80.25" ss:Span="2"/>
   <Column ss:Index="15" ss:Width="60.75"/>
   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s62"><Data ss:Type="String">Type</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">EmpCID</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">SerCID</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s62"><Data ss:Type="String">ProviderName</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">ProviderType</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">ServiceArea</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Department</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Specialty</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">UserType</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ReportingPeriodStartDate</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s64"><Data ss:Type="String">ReportingPeriodEndDate</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s65"><Data ss:Type="String">Metric</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s66"><Data ss:Type="String">Numerator</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s66"><Data ss:Type="String">Denominator</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s66"><Data ss:Type="String">Value</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
   </Row>
   <Row ss:AutoFitHeight="0" ss:Height="60">
    <Cell ss:StyleID="s67"><Data ss:Type="String">Provider</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="Number">123413</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="Number">234123</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">Person's Name</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">NURSE PRACTITIONER</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">CHOP SERVICE AREA</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">VIRTUA ORTHOPEDICS</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">Orthopedics</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">Non-Physician</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">7/28/2019</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">8/31/2019</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="String">Messages Received per Day - Encounter Report</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="Number">0</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="Number">21</Data></Cell>
    <Cell ss:StyleID="s67"><Data ss:Type="Number">0</Data></Cell>
   </Row>
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <Unsynced/>
   <Selected/>
   <FreezePanes/>
   <FrozenNoSplit/>
   <SplitHorizontal>1</SplitHorizontal>
   <TopRowBottomPane>1</TopRowBottomPane>
   <ActivePane>2</ActivePane>
   <Panes>
    <Pane>
     <Number>3</Number>
    </Pane>
    <Pane>
     <Number>2</Number>
     <ActiveCol>3</ActiveCol>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
  <AutoFilter x:Range="R1C1:R1C15"
   xmlns="urn:schemas-microsoft-com:office:excel">
  </AutoFilter>
 </Worksheet>
 <x:ExcelWorkbook/>
 <ss:UserWebMetaData>
   <ss:UserFirstName>Stephon</ss:UserFirstName>
   <ss:UserLastName>Proctor</ss:UserLastName>
   <ss:UserFirstEmail>proctors@email.chop.edu</ss:UserFirstEmail>
   <ss:UserID>29c3862c-57c8-487c-8f79-5d50e87af7a7</ss:UserID>
   <ss:Instant>9/24/2019 11:51:52 AM</ss:Instant>
  </ss:UserWebMetaData>
</Workbook>

【问题讨论】:

  • 你确定它是一个有效的 excel 文件吗?您是否尝试过使用 excel 打开重新保存? (当文件很多时,使用 vba-macro 来执行此操作)。 Alto,尝试使用 readxl-package 中的 read_excel()...

标签: r xml dataframe parsing


【解决方案1】:

考虑使用xmlToDataFrame 提取您的特定数据项,然后将标头迁移为前15 个提取项,最后将reshape 从长迁移到宽。由于 XML 维护一个默认名称空间,因此为解析分配了一个临时前缀 doc。下面假设 15 列非缺失 Cell/Data 节点的一致性。

library(XML)
fileurl <- "pep_slim.xml"

doc <- xmlParse(fileurl)     
nmsp <- c(doc="urn:schemas-microsoft-com:office:spreadsheet")    
df <- xmlToDataFrame(doc, nodes=getNodeSet(doc, "//doc:Cell", nmsp))

df$Headers <- df$Data[1:15]                  # ASSIGN HEADERS TO EVERY 15 ROWS
df <- with(df, df[Data != Headers,])         # SUBSET OUT ORIGINAL HEADERS

# ADD 1-15 ID SEQUENCE (FOR RESHAPING)
df$id <- with(df, ave(as.integer(NamedCell), Headers, FUN=seq_along))

# RESHAPE WIDE TO LONG WITH CLEANUP OF NAMES
rdf <- reshape(df, v.names = "Data", timevar="Headers", idvar="id",
               drop = "NamedCell", direction="wide", sep="_")

colnames(rdf) <- gsub("Data_", "", colnames(rdf))
rownames(rdf) <- NULL

输出

rdf
#   id     Type EmpCID SerCID  ProviderName       ProviderType       ServiceArea         Department   Specialty      UserType
# 1  1 Provider 123413 234123 Person's Name NURSE PRACTITIONER CHOP SERVICE AREA VIRTUA ORTHOPEDICS Orthopedics Non-Physician
#   ReportingPeriodStartDate ReportingPeriodEndDate                                       Metric Numerator Denominator Value
# 1                7/28/2019              8/31/2019 Messages Received per Day - Encounter # Report         0          21     0

【讨论】:

  • YASSSS!!这是完美的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多