首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中使用pdftools提取字符串后的特定表

在R中使用pdftools提取字符串后的特定表
EN

Stack Overflow用户
提问于 2020-10-10 13:23:40
回答 1查看 405关注 0票数 0

我有几个pdf,我希望提取股东表。我如何指定只提取出现在字符串‘二十个最大股东’之后的表?

我试过了,但对功能部分不是很确定。

代码语言:javascript
复制
library("pdftools")
library("tidyverse")

url <- c("https://www.computershare.com/News/Annual%20Report%202019.pdf?2")

raw_text <- map(url, pdf_text)


clean_table <- function(table){
  table <- str_split(table, "\n", simplify = TRUE)
  table_start <- stringr::str_which(table, "TWENTY LARGEST SHAREHOLDERS")
  table <- table[1, (table_start +1 ):(table_end - 1)]
  table <- str_replace_all(table, "\\s{2,}", "|")
  text_con <- textConnection(table)
  data_table <- read.csv(text_con, sep = "|")
  colnames(data_table) <- c("Name", "Number of Shares", "Percentage")
}

shares <- map_df(raw_text, clean_table) 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-10 21:01:52

尝尝这个。除了一些小问题之外,主要的变化是我首先得到了包含所需表的页面。顺便说一句:你必须搜索“20大股东”,而不是“20大股东”。

代码语言:javascript
复制
library(pdftools)
library(tidyverse)

# download pdf
url <- c("https://www.computershare.com/News/Annual%20Report%202019.pdf?2")

raw_text <- map(url, pdf_text)

clean_table1 <- function(raw) {
  
  # Split the single pages
  raw <- map(raw, ~ str_split(.x, "\\n") %>% unlist())
  # Concatenate the splitted pages
  raw <- reduce(raw, c)
  
  table_start <- stringr::str_which(tolower(raw), "twenty largest shareholders")
  table_end <- stringr::str_which(tolower(raw), "total")
  table_end <- table_end[min(which(table_end > table_start))]
  
  table <- raw[(table_start + 3 ):(table_end - 1)]
  table <- str_replace_all(table, "\\s{2,}", "|")
  text_con <- textConnection(table)
  data_table <- read.csv(text_con, sep = "|")
  colnames(data_table) <- c("Name", "Number of Shares", "Percentage")
  data_table
}

shares <- map_df(raw_text, clean_table1) 
head(shares)
#>                                                    Name Number of Shares
#> 1             J P Morgan Nominees Australia Pty Limited      109,500,852
#> 2                         Citicorp Nominees Pty Limited       57,714,777
#> 3                                       Mr Chris Morris       32,231,000
#> 4                             National Nominees Limited       19,355,892
#> 5                                         Welas Pty Ltd       18,950,000
#> 6 BNP Paribas Nominees Pty Ltd <Agency Lending DRP A/C>       11,520,882
#>   Percentage
#> 1      20.17
#> 2      10.63
#> 3       5.94
#> 4       3.56
#> 5       3.49
#> 6       2.12
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64290260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档