Find characters in a text
<p>I want to create a python script which finds an expression in a text.
If it succeeds, then print 'expression found'.</p>

<p>My text file is named "file_id_ascii"; it comes from a link named "clinical_file_link".</p>

<pre><code> import csv
 import sys
 import io

 file_id_ascii = 
     io.open(clinical_file_link, 'r',
             encoding='us-ascii', errors='ignore')
 acc = 'http://tcga.nci/bcr/xml/clinical/acc/'

 if acc in file_id_ascii:
     print('expression found')
</code></pre>

<p>That code doesn't work...what's wrong?</p>

<p>It doesn't work because you are not parsing the XML.
Check this <a href="http://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python">thread</a> for more info about parsing XML.</p>

<p>If it is indeed a text file you might edit it like this:</p>

<pre><code>if acc in open(clinical_file_link.txt).read(): 

     print('expression FOUND')
</code></pre>

<p>EDIT: This solution wont work if files are big. Try this one:</p>

<pre><code>with open('clinical_file_link.txt') as f:
    found = False
    for line in f:
        if acc in line:
            print('found')
            found = True
    if not found:
        print('The string cannot be found!')
</code></pre>

python xml csv parsing