0

I want to create a python script which finds an expression in a text. If it succeeds, then print 'expression found'.

My text file is named "file_id_ascii"; it comes from a link named "clinical_file_link".

 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')

That code doesn't work...what's wrong?

martineau
  • 119,623
  • 25
  • 170
  • 301
Nick Yellow
  • 329
  • 1
  • 3
  • 7
  • 2
    because you opened the file but did not read it using `read()` function. have a look at this: http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python – HiFile.app - best file manager Oct 12 '16 at 16:39
  • thank you but this not solve my problem still here – Nick Yellow Oct 12 '16 at 16:49
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your code and accurately describe the problem. "doesn't work" is not a problem description. – Prune Oct 12 '16 at 17:15
  • 1
    Your "doesn't work" may be an accurate description, but it does not help us help you. We need more detail. Do we need to start with "is your computer switched on?" or do you want to add more detail? – Jongware Oct 12 '16 at 17:24

2 Answers2

2
if 'http://tcga.nci/bcr/xml/clinical/acc/' \
    in open('clinical_file_link.txt').read():
    print "true"

... works if your file has small content

Prune
  • 76,765
  • 14
  • 60
  • 81
codeZach
  • 39
  • 5
1

It doesn't work because you are not parsing the XML. Check this thread for more info about parsing XML.

If it is indeed a text file you might edit it like this:

if acc in open(clinical_file_link.txt).read(): 

     print('expression FOUND')

EDIT: This solution wont work if files are big. Try this one:

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!')
Community
  • 1
  • 1
nephilimrising
  • 135
  • 3
  • 18