I have written a crude Python program to pull phrases from an index in a CSV file and write these rows to another 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)
for 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 is this: Isn’t there a better (more elegant/less verbose) Pythonic way to do this? I feel this is a case of not knowing what I don’t know. The CSV file I’m searching is not large (3863 lines, 669 KB) so I don’t think it is necessary to use SQL to solve this, although I am certainly open to that.
I am a Python newbie, in love with the language and teaching myself through the normal channels (books, tutorials, Project Euler, Stack Overflow).
Any suggestions are greatly appreciated.
It’s not a huge improvement, but you could do something like
(not tested)