簡體   English   中英

如何使用python提取文本文件中的特定段落並將其保存在csv文件中?

[英]How to extract specific paragraph in a text file and save it in csv file using python?

我有一個包含標題、作者、摘要、DOI 等信息的文本文件。我只想提取摘要並將其存儲在 dataframe 中。我嘗試使用下面的代碼,但我得到了作者信息和 DOI,我只想要Author information:DOI:之間的中間段落。 如何獲取該特定段落並將其存儲在 dataframe 中

extracted_lines=[]
extract = False

for line in open("abstract.txt"):

    if extract == False and "Author information:" in line.strip():
        extract = True
        
    if extract:
        extracted_lines.append(line)
        if "DOI:" in line.strip():
            extract = False
            
print("".join(extracted_lines))


**Output**

Author information:
(1)Carol Davila University of Medicine and Pharmacy, 37, Dionisie Lupu St, 
Bucharest, Romania 020021.
(2)National Institute of Public Health, 1-3 Doctor Leonte Anastasievici St, 
Bucharest, Romania 050463.

Dark chocolate is not the most popular chocolate; the higher concentration in 
antioxidants pays tribute to the increment in bitterness. The caloric density of 
dark chocolate is potentially lower but has a large variability according to 
recipes and ingredients. Nevertheless, in the last decade, the interest in dark 
chocolate as a potential functional food has constantly increased. In this 
review, we present the nutritional composition, factors influencing the 
bioavailability, and health outcomes of dark chocolate intake. We have extracted 
pro- and counter-arguments to illustrate these effects from both experimental 
and clinical studies in an attempt to solve the dilemma. The antioxidative and 
anti-inflammatory abilities, the cardiovascular and metabolic effects, and 
influences on central neural functions were selected to substantiate the main 
positive consequences. Beside the caloric density, we have included reports 
placing responsibility on chocolate as a migraine trigger or as an inducer of 
the gastroesophagial reflux in the negative effects section. Despite an 
extensive literature review, there are not large enough studies specifically 
dedicated to dark chocolate that took into consideration possible confounders on 
the health-related effects. Therefore, a definite answer on our initial question 
is, currently, not available.

DOI: 10.5740/jaoacint.19-0132
Author information:
(1)School of Food Science and Nutrition, Faculty of Maths and Physical Sciences, 
University of Leeds, Leeds LS2 9JT, UK.
(2)School of Food Science and Nutrition, Faculty of Maths and Physical Sciences, 
University of Leeds, Leeds LS2 9JT, UK. Electronic address: 
g.williamson@leeds.ac.uk.

Dark chocolate contains many biologically active components, such as catechins, 
procyanidins and theobromine from cocoa, together with added sucrose and lipids. 
All of these can directly or indirectly affect the cardiovascular system by 
multiple mechanisms. Intervention studies on healthy and 
metabolically-dysfunctional volunteers have suggested that cocoa improves blood 
pressure, platelet aggregation and endothelial function. The effect of chocolate 
is more convoluted since the sucrose and lipid may transiently and negatively 
impact on endothelial function, partly through insulin signalling and nitric 
oxide bioavailability. However, few studies have attempted to dissect out the 
role of the individual components and have not explored their possible 
interactions. For intervention studies, the situation is complex since suitable 
placebos are often not available, and some benefits may only be observed in 
individuals showing mild metabolic dysfunction. For chocolate, the effects of 
some of the components, such as sugar and epicatechin on FMD, may oppose each 
other, or alternatively in some cases may act together, such as theobromine and 
epicatechin. Although clearly cocoa provides some cardiovascular benefits 
according to many human intervention studies, the exact components, their 
interactions and molecular mechanisms are still under debate.

Copyright © 2015 Elsevier Inc. All rights reserved.

DOI: 10.1016/j.vph.2015.05.011

Expected Output

Index    Abstract
    0      Dark chocolate is not the most popular chocola...
    1      Dark chocolate contains many biologically acti...

你可以試試:

  • 以字符串形式檢索文件的全部內容
  • 拆分“作者信息:\n”,以檢索有關每篇論文的信息
  • 獲取論文的索引 1,以檢索摘要

這是代碼:

with open("abstract.txt") as f:
    contents = f.read()

papers = [p for p in contents.split('Author information:\n')]
abstracts = [p.split("\n\n")[1] for p in papers[1:]

對你起作用嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM