You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R语言中提取带编号TXT文件内容并转换为数据框

在R中提取TXT文件中按数字编号分隔的文本

我有一个TXT文件,内容以1.、2.……400.这样的数字编号分隔列表项,每个编号对应一段多行文本。需要把每个编号对应的文本提取为单独的观测,最终生成一个每行对应一段文本的数据框。

以下是实际示例文本:

1. 
Readability and Quality of Online Information on Sickle Cell Retinopathy for 
Patients [embase.com]
Gbedemah Z.E.E., Fuseini M.-S.N., Fordjuor S.K.E.J., Baisie-Nkrumah E.J., Beecham R.-
M.E.M., Amissah-Arthur K.N. 
[In Process] Am. J. Ophthalmol. 2024 259: (45-52) 
Embase, MEDLINE
Go to publisher for the full text [dx.doi.org]
Embase Open URL: redirect to full text [embase.com]
Abstract
PURPOSE: This study aims to evaluate the readability and quality of Internet-based health 
information on sickle cell retinopathy. DESIGN: Retrospective cross-sectional website analysis. 
METHODS: To simulate a patient's online search, the terms “sickle cell retinopathy” and “sickle 
cell disease in the eye” were entered into the top 3 search engines (Google, Bing and Yahoo). The 
first 20 results of each search were retrieved and screened for analysis. The DISCERN 
questionnaire, the Journal of the American Medical Association (JAMA) standards, and the Health 
on the Net (HON) criteria were used to evaluate the quality of the information. The Flesch–Kincaid 
Grade Level (FKGL), the Flesch Reading Ease (FRES), and the Automated Readability Index 
(ARI) were used to assess the readability of each website. RESULTS: Of 16 online sources, 12 
(75%) scored moderately on the DISCERN tool. The mean DISCERN score was 40.91 (SD, 
10.39; maximum possible, 80). None of the sites met all of the JAMA benchmarks, and only 3 
(18.75%) of the websites had HONcode certification. All of the websites had scores above the 
target American Medical Association grade level of 6 on both the FKGL and ARI. The mean FRES 
was 57.76 (±4.61), below the recommended FRES of 80 to 90. CONCLUSION: There is limited 
online information available on sickle cell retinopathy. Most included websites were fairly difficult to 
read and of substandard quality. The quality and readability of Internet-based, patient-focused 
information on sickle cell retinopathy needs to be improved.


 
2. 
Multicomponent Strategy Improves Human Papillomavirus Vaccination Rates 
Among Adolescents with Sickle Cell Disease [embase.com]
Aurora T., Cole A., Rai P., Lavoie P., McIvor C., Klesges L.M., Kang G., Liyanage J.S.S., 
Brandt H.M., Hankins J.S. 
J. Pediatr. 2024 265: 
Embase, MEDLINE, NURSING
Go to publisher for the full text [dx.doi.org]
Embase Open URL: redirect to full text [embase.com]
Abstract
Objective: To evaluate the effectiveness of a vaccine strategy bundle to increase human 
papillomavirus (HPV) vaccine initiation and completion in a specialty clinic setting. Study design: 
Our Hematology clinic utilized an implementation framework from October 1, 2018, to December 
31, 2019, involving nurses, nursing coordinators, and clinicians in administering the HPV 
vaccination series to our adolescent sickle cell sample of nearly 500 patients. The bundle included 
education for staff on the need for HPV vaccine administration, provider incentives, vaccines 
offered to patients in SCD clinics, and verification of patients' charts of vaccine completion. 
Results: Following the implementation of the bundle, the cumulative incidence of HPV vaccination 
initiation and completion improved from 28% to 46% and 7% to 49%, respectively. Both rates 
remained higher postimplementation as well. HPV vaccination series completion was associated 
with a decreased distance to the health care facility, lower state deprivation rank, and increased 
hospitalizations. Conclusion: Our clinic's implementation strategy successfully improved vaccine 
completion rates among adolescents with sickle cell disease (SCD) while continuing to educate 
staff, patients, and families on the importance of cancer prevention among people living with SCD.

我的初步尝试代码:

# Sample character string
cleaned_text <- "1. Readability and Quality of Online Information on Sickle Cell Retinopathy for Patients [embase.com] Gbedemah 2. Another text here 3. Yet another text 4. More text 5. Even more text"

# Split the text based on the numbering pattern
text_split <- strsplit(cleaned_text, "\\d+\\.\\s+")[[1]]

# Remove the first empty element
text_split <- text_split[-1]

# Convert the result into a data frame
df <- data.frame(Text = text_split)

修正后的实现方案

由于实际文本包含多行换行和多余空白,需要先完整读取文件并处理格式,再进行分割:

完整代码

# 读取TXT文件,将所有行合并为单个字符串(保留换行可将collapse改为"\n")
raw_text <- paste(readLines("your_file.txt"), collapse = " ")

# 按数字编号分割文本:匹配编号前的任意空白/换行 + 数字 + 点号 + 空白
text_split <- strsplit(raw_text, "\\s*\\n*\\d+\\.\\s+")[[1]]

# 移除分割后开头的空元素
text_split <- text_split[-1]

# 清理文本中的连续空白(统一替换为单个空格)
text_cleaned <- gsub("\\s+", " ", text_split)

# 生成带编号的数据框
df <- data.frame(
  Item_Number = seq_along(text_cleaned),
  Text = text_cleaned,
  stringsAsFactors = FALSE
)

# 查看结果
head(df)

关键说明

  • 正则表达式\\s*\\n*\\d+\\.\\s+:覆盖编号前可能存在的换行、空白,确保准确识别所有位置的编号分隔符。
  • gsub("\\s+", " ", text_split):将文本中的连续空格、换行统一替换为单个空格,避免格式混乱;若需保留原始换行,可跳过此步骤。
  • 若要保留原始文本的换行格式,将paste(readLines(...), collapse = " ")改为paste(readLines(...), collapse = "\n")即可。

内容的提问来源于stack exchange,提问作者sebastian.mendoza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 20:44:51