Home > database >  How to extract specific paragraph in a text file and save it in csv file using python?
How to extract specific paragraph in a text file and save it in csv file using python?

Time:04-16

I have a text file which contains the information about Title, Author, Abstract, DOI etc. I want to extract only the abstract and store it in a dataframe. I tried using below code, but I'm getting Author information and DOI, I only want the middle paragraph between Author information: and DOI:. How do I get that specific paragraph and store it in a 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: 
[email protected].

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...

CodePudding user response:

You can try:

  • retrieving the whole content of the file as a string
  • splitting on 'Author information:\n', to retrieve infos about every single paper
  • getting the index 1 of your papers, to retrieve the abstracts

Here's the code:

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:]

Does it work for you?

  • Related