Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: Yomna91 on Feb 20th, 2013  |  syntax: None  |  size: 0.95 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from whoosh import fields, index
  2. import os.path
  3. import csv
  4. import codecs
  5.  
  6. # This list associates a name with each position in a row
  7. columns = ["juza","chapter","verse","analysis"]
  8.  
  9. schema = fields.Schema(juza=fields.NUMERIC,
  10.                        chapter=fields.NUMERIC,
  11.                        verse=fields.NUMERIC,
  12.                        analysis=fields.KEYWORD)
  13.                              
  14.  
  15. # Create the Whoosh index
  16. indexname = "index"
  17. if not os.path.exists(indexname):
  18.   os.mkdir(indexname)
  19. ix = index.create_in(indexname, schema)
  20.  
  21. # Open a writer for the index
  22. with ix.writer() as writer:
  23.   # Open the CSV file
  24.  with codecs.open("yom.csv", "rb","utf8") as csvfile:
  25.     for line in csvfile:
  26.         juza , chapter, verse , analysis = line.split('   ', 3)
  27.         doc = {k:v.strip() for k,v in zip(columns, analysis.split(':'))}
  28.  
  29.  
  30.       # Pass the dictionary to the add_document method
  31.         writer.add_document(**doc)
  32.     writer.commit()