Home > Software design >  How to iterate line by line over element of the list in Python?
How to iterate line by line over element of the list in Python?

Time:10-27

I have the following code to search over the text file and extract the text parts between certain elements: start="a owl:Class" end=' .\n' and append these text parts as elements of the list.

contents = []
with open(r'C:/Users/Jupyter Notebooks/small.ttl', 'r', encoding="UTF-8") as f:
   recording = False
   content = ''
   for line in f:
      if start in line:
         recording = True
      if recording and end in line:
         recording = False
         contents.append(content)
      if recording:
         content  = line
      else:
         content = ''
for i in contents
    print (i)

The list contents contains two elements which are consisting of strings of text. Every line of text starts with a new line First element:

http://purl.bioontology.org/ontology/SNOMEDCT/1075251000119104 a owl:Class ; skos:prefLabel """Acquired left metatarsus adductus"""@en ; skos:notation """1075251000119104"""^^xsd:string ; skos:altLabel """Acquired left metatarsus adductus (disorder)"""@en ; http://purl.bioontology.org/ontology/SNOMEDCT/has_finding_site http://purl.bioontology.org/ontology/SNOMEDCT/726438004 ; http://purl.bioontology.org/ontology/SNOMEDCT/has_associated_morphology http://purl.bioontology.org/ontology/SNOMEDCT/767172008 ; http://purl.bioontology.org/ontology/SNOMEDCT/occurs_in http://purl.bioontology.org/ontology/SNOMEDCT/767023003 ; rdfs:subClassOf http://purl.bioontology.org/ontology/SNOMEDCT/99701000119102 ; rdfs:subClassOf http://purl.bioontology.org/ontology/SNOMEDCT/774124003 ;

Second element:

http://purl.bioontology.org/ontology/SNOMEDCT/10308009 a owl:Class ; skos:prefLabel """Argon-42"""@en ; skos:notation """10308009"""^^xsd:string ; skos:altLabel """42-Ar"""@en , """Argon-42 (substance)"""@en ; rdfs:subClassOf http://purl.bioontology.org/ontology/SNOMEDCT/35016001 ; http://purl.bioontology.org/ontology/SNOMEDCT/SUBSET_MEMBER """900000000000508004~ACCEPTABILITYID~900000000000548007"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/TYPE_ID """900000000000013009"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/CASE_SIGNIFICANCE_ID """900000000000017005"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/SUBSET_MEMBER """900000000000509007~ACCEPTABILITYID~900000000000548007"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/INACTIVATION_INDICATOR """723277005"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/SUBSET_MEMBER """900000000000490003~VALUEID~723277005"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/SUBSET_MEMBER """900000000000509007~ACCEPTABILITYID~900000000000548007"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/TYPE_ID """900000000000013009"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/CASE_SIGNIFICANCE_ID """900000000000448009"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/SUBSET_MEMBER """900000000000508004~ACCEPTABILITYID~900000000000548007"""^^xsd:string ;

I want to go through every list element line by line and check whether the line contains certain string. For example:

if "a:owl Class" in line:
                  print line

My problem is that I'm not able to iterate over lines of the elements of the list.

CodePudding user response:

So I did this.

First I converted your 2 elements into strings and put them into a list.

string1 = 'http://purl.bioontology.org/ontology/SNOMEDCT/1075251000119104 a owl:Class ; skos:prefLabel """Acquired left metatarsus adductus"""@en ; skos:notation """1075251000119104"""^^xsd:string ; skos:altLabel """Acquired left metatarsus adductus (disorder)"""@en ; http://purl.bioontology.org/ontology/SNOMEDCT/has_finding_site http://purl.bioontology.org/ontology/SNOMEDCT/726438004 ; http://purl.bioontology.org/ontology/SNOMEDCT/has_associated_morphology http://purl.bioontology.org/ontology/SNOMEDCT/767172008 ; http://purl.bioontology.org/ontology/SNOMEDCT/occurs_in http://purl.bioontology.org/ontology/SNOMEDCT/767023003 ; rdfs:subClassOf http://purl.bioontology.org/ontology/SNOMEDCT/99701000119102 ; rdfs:subClassOf http://purl.bioontology.org/ontology/SNOMEDCT/774124003 ;'
string2 = 'http://purl.bioontology.org/ontology/SNOMEDCT/10308009 a owl:Class ; skos:prefLabel """Argon-42"""@en ; skos:notation """10308009"""^^xsd:string ; skos:altLabel """42-Ar"""@en , """Argon-42 (substance)"""@en ; rdfs:subClassOf http://purl.bioontology.org/ontology/SNOMEDCT/35016001 ; http://purl.bioontology.org/ontology/SNOMEDCT/SUBSET_MEMBER """900000000000508004~ACCEPTABILITYID~900000000000548007"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/TYPE_ID """900000000000013009"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/CASE_SIGNIFICANCE_ID """900000000000017005"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/SUBSET_MEMBER """900000000000509007~ACCEPTABILITYID~900000000000548007"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/INACTIVATION_INDICATOR """723277005"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/SUBSET_MEMBER """900000000000490003~VALUEID~723277005"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/SUBSET_MEMBER """900000000000509007~ACCEPTABILITYID~900000000000548007"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/TYPE_ID """900000000000013009"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/CASE_SIGNIFICANCE_ID """900000000000448009"""^^xsd:string ; http://purl.bioontology.org/ontology/SNOMEDCT/SUBSET_MEMBER """900000000000508004~ACCEPTABILITYID~900000000000548007"""^^xsd:string ;'
contents = [string1,string2]

You should be able to reproduce my code from this part onwards.

First I iterate over each item in the list.

Then I split each line into a list using the ; as the separator.

Then I iterate each list over each item in the list looking for your string.

for content in contents:
    for line in content.split(';'):
        if 'a owl:Class' in line:
            print(i)

Here is my output,

http://purl.bioontology.org/ontology/SNOMEDCT/1075251000119104 a owl:Class 
http://purl.bioontology.org/ontology/SNOMEDCT/10308009 a owl:Class 
  • Related