Advertisement
Yomna91

Untitled

Feb 20th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement