list - Is there a better way to read an element from a file in Python? -


i have written crude python program pull phrases index in csv file , write these rows file.

import csv  total = 0  ifile = open('data.csv', "rb") reader = csv.reader(ifile)  ofile = open('newdata_write.csv', "wb") writer = csv.writer(ofile, delimiter='\t', quotechar='"', quoting=csv.quote_all)  row in reader:     if ("some text") in row[x]:         total = total + 1         writer.writerow(row)     elif ("some more text") in row[x]:         total = total + 1            writer.writerow(row)      elif ("even more text i'm looking for") in row[x]:           total = total + 1            writer.writerow(row)     < many, many more lines >  print "\ntotal = %d." % total  ifile.close() 

my question this: isn't there better (more elegant/less verbose) pythonic way this? feel case of not knowing don't know. csv file i'm searching not large (3863 lines, 669 kb) don't think necessary use sql solve this, although open that.

i python newbie, in love language , teaching myself through normal channels (books, tutorials, project euler, stack overflow).

any suggestions appreciated.

it's not huge improvement, like

keyphraselist = (      "some text",      "some more text",      "even more text i'm looking for")  ... row in reader:    phrase in keyphraselist:        if phrase in row[x]:            total = total + 1            writer.writerow(row)            break 

(not tested)


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -