def csv2tree(source,
             delimiter=",",
             quotechar='"'):

    # Increase field size to be able to import WKTs
    csv.field_size_limit(2**20 * 100)  # 100 megs

    # Shortcuts
    SubElement = etree.SubElement

    root = etree.Element(TABLE)

    def add_col(row, key, value, hashtags=None):

        col = SubElement(row, COL)
        col.set(FIELD, s3_unicode(key))
        if hashtags:
            hashtag = hashtags.get(key)
            if hashtag and hashtag[1:]:
                col.set(HASHTAG, hashtag)
        if value:
            text = s3_unicode(value).strip()
            if text[:6].lower() not in ("null", "<null>"):
                col.text = text
        else:
            col.text = ""

    def utf_8_encode(source):

        encodings = ["utf-8-sig", "iso-8859-1"]
        e = encodings[0]
        for line in source:
            if e:
                try:
                    yield unicode(line, e, "strict").encode("utf-8")
                except:
                    pass
                else:
                    continue
            for encoding in encodings:
                try:
                    yield unicode(line, encoding, "strict").encode("utf-8")
                except:
                    continue
                else:
                    e = encoding
                    break

    hashtags = {}

    import StringIO
    if not isinstance(source, StringIO.StringIO):
        source = utf_8_encode(source)
    reader = csv.DictReader(source,
                            delimiter=delimiter,
                            quotechar=quotechar)

    for i, r in enumerate(reader):
        # Skip empty rows
        if not any(r.values()):
            continue
        if i == 0:
            # Auto-detect hashtags
            items = {}
            for k, v in r.items():
                if v:
                    try:
                        v = v.strip()
                    except __HOLE__: # v is a List
                        v = s3_unicode(v)
                    items[k] = v
            if all(v[0] == '#' for v in items.values()):
                hashtags.update(items)
                continue
        row = SubElement(root, ROW)
        for k in r:
            add_col(row, k, r[k], hashtags=hashtags)

    return  etree.ElementTree(root)

# -----------------------------------------------------------------------------
dataset/ETHPy150Open sahana/eden/static/scripts/tools/csv2xml.py/csv2tree