import csv
import json


class Example(object):
    """Defines a single training or test example.

    Stores each column of the example as an attribute.
    """

    @classmethod
    def fromJSON(cls, data, fields):
        return cls.fromdict(json.loads(data), fields)

    @classmethod
    def fromdict(cls, data, fields):
        ex = cls()
        for key, vals in fields.items():
            if key in data and vals is not None:
                if not isinstance(vals, list):
                    vals = [vals]
                for val in vals:
                    name, field = val
                    setattr(ex, name, field.preprocess(data[key]))
        return ex

    @classmethod
    def fromTSV(cls, data, fields):
        if data[-1] == '\n':
            data = data[:-1]
        return cls.fromlist(data.split('\t'), fields)

    @classmethod
    def fromCSV(cls, data, fields):
        if data[-1] == '\n':
            data = data[:-1]
        return cls.fromlist(list(csv.reader([data]))[0], fields)

    @classmethod
    def fromlist(cls, data, fields):
        ex = cls()
        for (name, field), val in zip(fields, data):
            if field is not None:
                setattr(ex, name, field.preprocess(val))
        return ex

    @classmethod
    def fromtree(cls, data, fields, subtrees=False):
        try:
            from nltk.tree import Tree
        except ImportError:
            print("Please install NLTK. "
                  "See the docs at http://nltk.org for more information.")
            raise
        tree = Tree.fromstring(data)
        if subtrees:
            return [cls.fromlist(
                [t.leaves(), t.label()], fields) for t in tree.subtrees()]
        return cls.fromlist([tree.leaves(), tree.label()], fields)

@@ -17,16 +17,28 @@
 t json%0A%0A
+import six%0A%0A
 %0Aclass E
@@ -967,55 +967,589 @@
     
-return cls.fromlist(list(csv.reader(%5Bdata%5D))%5B0%5D
+# If Python 2, encode to utf-8 since CSV doesn't take unicode input%0A        if six.PY2:%0A            data = data.encode('utf-8')%0A        # Use Python CSV module to parse the CSV line%0A        parsed_csv_lines = csv.reader(%5Bdata%5D)%0A%0A        # If Python 2, decode back to unicode (the original input format).%0A        if six.PY2:%0A            for line in parsed_csv_lines:%0A                parsed_csv_line = %5Bsix.text_type(col, 'utf-8') for col in line%5D%0A                break%0A        else:%0A            parsed_csv_line = list(parsed_csv_lines)%5B0%5D%0A        return cls.fromlist(parsed_csv_line
 , fi

Fix CSV parsing as csv module can't take unicode in Py2