Separating a text document by specific lines of text using python

CiaranWelsh

I'm writing a python function to take a chunk of text, parsed from a text file using f.readlines and split this chunk of text into a list. The text contains dividers and I want to split this text specifically at these locations. Below is an example of the text file in question.

@model:2.4.0=Skeleton "Skeleton"
@compartments
 Cell=1.0 "Cell"
@species
 Cell:[A]=100.0 "A"
 Cell:[B]=1.0 "B"
 Cell:[C]=0.0 "C"
 Cell:[D]=0.0 "D"
@parameters
kcat=4000
km = 146
v2_k = 88
@reactions
@r=v1 "v1"
 A -> C : B
 Cell * kcat * B * A / (km + A) 
@r=v2 "v2"
 C -> C+D
 Cell * v2_k * C

My desired output is to have a python dictionary that has the name of the dividers as keys and all the content between that divider and the next as values. For example, the first element of the sections dictionary should be:

sections['@model']=:2.4.0=Skeleton "Skeleton"

Current Code

def split_sections(SBshorthand_file):
    '''
    Takes a SBshorthand file and returns a dictionary of each of the sections. 
    Keys of the dictionary are the dividers.
    Values of dictionary are the content between dividers. 
    '''
    SBfile=parse_SBshorthand_read(SBshorthand_file) #simple parsing function. uses f.read()
    dividers=["@model", "@units", "@compartments", "@species", "@parameters", "@rules", "@reactions", "@events"]
    sections={}
    for i in  dividers:
        pattern=re.compile(i)
        if re.findall(pattern,SBfile) == []:
            pass
#            print 'Section \'{}\' not present in {}'.format(i,SBshorthand_file)
        else:
            SBfile2=re.sub(pattern,'\n'+i,SBfile)
            print SBfile2

This however does not do what I want. Would anybody have any ideas how to fix my code? Thanks

-----------------Edit--------------------

Please note that the section '@reactions' contains a number of 'reactions' all of which start with @r, but they all need to be grouped under the reactions key.

vks
import re

x="""@model:2.4.0=Skeleton "Skeleton"
@compartments
Cell=1.0 "Cell"
@species
Cell:[A]=100.0 "A"
Cell:[B]=1.0 "B"
Cell:[C]=0.0 "C"
Cell:[D]=0.0 "D"
@parameters
kcat=4000
km = 146
v2_k = 88
@reactions
@r=v1 "v1"
A -> C : B
Cell * kcat * B * A / (km + A)
@r=v2 "v2"
C -> C+D
Cell * v2_k * C"""


print dict(re.findall(r"(?:^|(?<=\n))(@\w+)([\s\S]*?)(?=\n@(?!r\b)\w+|$)",x))

You can directly use re.findall and get what you want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find specific lines in text document

From Dev

Rename specific lines in a text file using python

From Dev

Rename specific lines in a text file using python

From Dev

Extract specific lines from text file using python

From Dev

Copy a specific text from lines using batch

From Dev

Python - combine text files (specific lines)

From Dev

Removing lines above specific line in text in python

From Dev

How to read text file lines after Specific lines using StreamReader

From Dev

Is there a faster way to insert text into a document with multiple lines using the ECHO command?

From Dev

Split text lines in scanned document

From Dev

python help separating lists in a text file

From Dev

how to read specific lines of a text document and write them to another text | C

From Dev

Comment some lines in a text file using python

From Dev

reading from a text document in python using a variable

From Dev

print specific lines from text file using perl

From Dev

Insert multiple lines of text before specific line using Bash

From Dev

Delete specific lines in a text file using vb.net

From Dev

Using perl to append text to specific lines in a file on Solaris

From Dev

Using perl to append text to specific lines in a file on Solaris

From Dev

Editing nested text and specific lines within a file using bash script

From Dev

print specific lines from text file using perl

From Dev

Printing specific lines from text file using cmd

From Dev

Mark specific lines of paragraph text

From Dev

Insert text in specific lines of a file

From Dev

Extract specific text from a document using notepad++

From Dev

Python - Read specific lines in a text file based on a condition

From Dev

Regex to filter and remove specific multiple lines of text from a file with python

From Dev

Separating then Joining Text and Data

From Dev

Separating banner from text

Related Related

  1. 1

    Find specific lines in text document

  2. 2

    Rename specific lines in a text file using python

  3. 3

    Rename specific lines in a text file using python

  4. 4

    Extract specific lines from text file using python

  5. 5

    Copy a specific text from lines using batch

  6. 6

    Python - combine text files (specific lines)

  7. 7

    Removing lines above specific line in text in python

  8. 8

    How to read text file lines after Specific lines using StreamReader

  9. 9

    Is there a faster way to insert text into a document with multiple lines using the ECHO command?

  10. 10

    Split text lines in scanned document

  11. 11

    python help separating lists in a text file

  12. 12

    how to read specific lines of a text document and write them to another text | C

  13. 13

    Comment some lines in a text file using python

  14. 14

    reading from a text document in python using a variable

  15. 15

    print specific lines from text file using perl

  16. 16

    Insert multiple lines of text before specific line using Bash

  17. 17

    Delete specific lines in a text file using vb.net

  18. 18

    Using perl to append text to specific lines in a file on Solaris

  19. 19

    Using perl to append text to specific lines in a file on Solaris

  20. 20

    Editing nested text and specific lines within a file using bash script

  21. 21

    print specific lines from text file using perl

  22. 22

    Printing specific lines from text file using cmd

  23. 23

    Mark specific lines of paragraph text

  24. 24

    Insert text in specific lines of a file

  25. 25

    Extract specific text from a document using notepad++

  26. 26

    Python - Read specific lines in a text file based on a condition

  27. 27

    Regex to filter and remove specific multiple lines of text from a file with python

  28. 28

    Separating then Joining Text and Data

  29. 29

    Separating banner from text

HotTag

Archive