[Checkins] SVN: z3c.csvvocabulary/trunk/s encoding of csv files can be specified

Harald Friessnegger harald at webmeisterei.com
Tue Aug 18 09:31:45 EDT 2009


Log message for revision 102907:
  encoding of csv files can be specified

Changed:
  U   z3c.csvvocabulary/trunk/setup.py
  U   z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/README.txt
  U   z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/vocabulary.py

-=-
Modified: z3c.csvvocabulary/trunk/setup.py
===================================================================
--- z3c.csvvocabulary/trunk/setup.py	2009-08-18 06:29:00 UTC (rev 102906)
+++ z3c.csvvocabulary/trunk/setup.py	2009-08-18 13:31:45 UTC (rev 102907)
@@ -34,6 +34,8 @@
         '**********************\n'
         + '\n' +
         read('src', 'z3c', 'csvvocabulary', 'README.txt')
+        + '\n' +
+        read('CHANGES.txt')
         ),
     license = "ZPL 2.1",
     keywords = "zope3 vocabulary csv",

Modified: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/README.txt
===================================================================
--- z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/README.txt	2009-08-18 06:29:00 UTC (rev 102906)
+++ z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/README.txt	2009-08-18 13:31:45 UTC (rev 102907)
@@ -74,6 +74,24 @@
 I said, this is totally okay.
 
 
+Encoding
+````````
+
+By default the vocabulary expects the csv file to be latin1 encoded.
+
+  >>> csvfile = os.path.join(path, 'testing', 'data', 'utf-8.csv')
+  >>> wrongEncoding = z3c.csvvocabulary.CSVVocabulary(csvfile)
+  >>> print wrongEncoding.getTerm('ae').title.default
+  ä
+
+If you csv file has a different encoding you can specify it explicitly:
+
+  >>> utf8Encoded = z3c.csvvocabulary.CSVVocabulary(csvfile, encoding='utf-8')
+  >>> term = utf8Encoded.getTerm('ae')
+  >>> print term.title.default
+  ä
+
+
 CSV Message String Extraction
 -----------------------------
 
@@ -90,4 +108,6 @@
    u'sample-value2': [('...sample.csv', 2)],
    u'sample-value3': [('...sample.csv', 3)],
    u'sample-value4': [('...sample.csv', 4)],
-   u'sample-value5': [('...sample.csv', 5)]}
+   u'sample-value5': [('...sample.csv', 5)],
+   u'utf-8-ae': [('...utf-8.csv', 1)],
+   u'utf-8-oe': [('...utf-8.csv', 2)]}

Modified: z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/vocabulary.py
===================================================================
--- z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/vocabulary.py	2009-08-18 06:29:00 UTC (rev 102906)
+++ z3c.csvvocabulary/trunk/src/z3c/csvvocabulary/vocabulary.py	2009-08-18 13:31:45 UTC (rev 102907)
@@ -25,7 +25,7 @@
 _ = MessageFactory('zope')
 
 
-def CSVVocabulary(filename, messageFactory=_):
+def CSVVocabulary(filename, messageFactory=_, encoding='latin1'):
     # Create a prefix
     prefix = os.path.split(filename)[-1][:-4]
     # Open a file and read the data
@@ -34,7 +34,7 @@
     # Create the terms and the vocabulary
     terms = []
     for id, title in reader:
-        title = unicode(title, 'latin1')
+        title = unicode(title, encoding)
         term = vocabulary.SimpleTerm(
             id, title=messageFactory(prefix+'-'+id, default=title))
         terms.append(term)



More information about the Checkins mailing list