Home > Blockchain >  How to search for a specific word within a text?
How to search for a specific word within a text?

Time:08-15

I have a file, of type txt, with the following text:

The dataset is available at: https://archive.ics.uci.edu/ml/datasets.php The file name is Cancer_Data.xml This is one of three domains provided by the Oncology Institute that has repeatedly appeared in the machine learning literature.

I need to search within this text the word that accompanies the "xml". I tried to do the following implementation:

      import pandas as pd

      with open(local_arquivo, "r") as file_read:   
          for line in file_read:        
              var_split = line.split()
              for i in range(0, len(var_split)):
                  if(var_split[i].str.contains('xml')):
                      archive_name = var_split.iloc[i]   

The idea was to separate the text using the split function and then look for the part that contains the 'xml'. However, when I run it, the following error appears:

        AttributeError: 'str' object has no attribute 'str'

I would like the output to be:

archive_name = Cancer_Data.xml

CodePudding user response:

Try

if('xml' in var_split[i]):

source: https://docs.python.org/3/reference/expressions.html#in

  • Related