:param data:
    :param fieldnames:
    :param with_header:
    :param crlf:
    :param tsv:
    :return: unicode
181832
def dump_csv(data: List[dict], fieldnames: Sequence[str], with_header: bool = False, crlf: bool = False,
             tsv: bool = False) -> str:
    

    def force_str(v):
        # XXX: Double quotation behaves strangely... so replace (why?)
        return dump_json(v).replace('"', "'") if isinstance(v, (dict, list)) else v

    with io.StringIO() as sio:
        dialect = get_dialect_name(crlf, tsv)
        writer = csv.DictWriter(sio, fieldnames=fieldnames, dialect=dialect, extrasaction='ignore')
        if with_header:
            writer.writeheader()
        for x in data:
            writer.writerow({k: force_str(v) for k, v in x.items()})
        sio.seek(0)
        return sio.read()
1296291
def _validate_field_headers(reader):
        

        link = 'tools.ietf.org/html/rfc4180#section-2'

        for field in reader.fieldnames:
            if not isinstance(field, str):
                fail('All headers in your CSV payload must be '
                     'strings.', link)

        if 'type' not in reader.fieldnames:
            fail('A type header must be present in your CSV '
                 'payload.', link)
1073683
def format_unencoded(self, tokensource, outfile):
        
        x = self.xoffset
        y = self.yoffset
        if not self.nowrap:
            if self.encoding:
                outfile.write('<?xml version="1.0" encoding="%s"?>\n' %
                              self.encoding)
            else:
                outfile.write('<?xml version="1.0"?>\n')
            outfile.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" '
                          '"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/'
                          'svg10.dtd">\n')
            outfile.write('<svg xmlns="http://www.w3.org/2000/svg">\n')
            outfile.write('<g font-family="%s" font-size="%s">\n' %
                          (self.fontfamily, self.fontsize))
        outfile.write('<text x="%s" y="%s" xml:space="preserve">' % (x, y))
        for ttype, value in tokensource:
            style = self._get_style(ttype)
            tspan = style and '<tspan' + style + '>' or ''
            tspanend = tspan and '</tspan>' or ''
            value = escape_html(value)
            if self.spacehack:
                value = value.expandtabs().replace(' ', '&#160;')
            parts = value.split('\n')
            for part in parts[:-1]:
                outfile.write(tspan + part + tspanend)
                y += self.ystep
                outfile.write('</text>\n<text x="%s" y="%s" '
                              'xml:space="preserve">' % (x, y))
            outfile.write(tspan + parts[-1] + tspanend)
        outfile.write('</text>')

        if not self.nowrap:
            outfile.write('</g></svg>\n')
817762
def _read_tsv(cls, input_file, quotechar=None):
        """"""
        with open(input_file, "r", encoding="utf-8") as f:
            reader = csv.reader(f, delimiter="\t", quotechar=quotechar)
            lines = []
            for line in reader:
                if sys.version_info[0] == 2:
                    line = list(unicode(cell, 'utf-8') for cell in line)
                lines.append(line)
            return lines
66203
def load_csvf(fpath, fieldnames, encoding):
    
    with open(fpath, mode='r', encoding=encoding) as f:
        snippet = f.read(8192)
        f.seek(0)

        dialect = csv.Sniffer().sniff(snippet)
        dialect.skipinitialspace = True
        return list(csv.DictReader(f, fieldnames=fieldnames, dialect=dialect))
185593
def to_utf8(datas):
    
    res = datas
    if isinstance(datas, dict):
        res = {}
        for key, value in datas.items():
            key = to_utf8(key)
            value = to_utf8(value)
            res[key] = value

    elif isinstance(datas, (list, tuple)):
        res = []
        for data in datas:
            res.append(to_utf8(data))

    elif isinstance(datas, unicode):
        res = datas.encode('utf-8')

    return res
996006
def _format_data(data):
        # type: (Union[str, IO]) -> Union[Tuple[None, str], Tuple[Optional[str], IO, str]]
        
        if hasattr(data, 'read'):
            data = cast(IO, data)
            data_name = None
            try:
                if data.name[0] != '<' and data.name[-1] != '>':
                    data_name = os.path.basename(data.name)
            except (AttributeError, TypeError):
                pass
            return (data_name, data, "application/octet-stream")
        return (None, cast(str, data))
1067126
def save_tsv(p, vs):
    ''
    delim = options.get('delimiter', vs)
    trdict = tsv_trdict(vs)

    save_tsv_header(p, vs)

    with p.open_text(mode='a') as fp:
        for dispvals in genAllValues(vs.rows, vs.visibleCols, trdict, format=True):
            fp.write(delim.join(dispvals))
            fp.write('\n')

    status('%s save finished' % p)
203223
def iriref_to_str(self, ref: ShExDocParser.IRIREF) -> str:
        
        rval = ref.getText()[1:-1].encode('utf-8').decode('unicode-escape')
        return rval if ':' in rval or not self.base else self.base.val + rval
930355
def ensure_str(data: Union[str, bytes]) -> str:
    
    if isinstance(data, bytes):
        return str(data, 'utf-8')

    return data
48091
def read_tred_tsv(tsvfile):
    
    df = pd.read_csv(tsvfile, sep="\t", index_col=0, dtype={"SampleKey": str})
    return df
499199
def filter_headers(data):
    
    headers = {}
    for i in data:
        if i == 'Content-Type' or i == 'Host' or i[0] == 'x' or i[0] == 'X':
            headers[i] = data[i]
    return headers
1364253
def convert_unicode_to_str(data):
        
        if six.PY2 and isinstance(data, six.text_type):
            return data.encode('utf8')
        elif six.PY3 and isinstance(data, six.binary_type):
            return data.decode('utf8')
        elif isinstance(data, collections.Mapping):
            return dict((Util.convert_unicode_to_str(k), Util.convert_unicode_to_str(v))
                        for k, v in six.iteritems(data))
        elif isinstance(data, collections.Iterable) and not isinstance(data, (six.binary_type, six.string_types)):
            return type(data)(map(Util.convert_unicode_to_str, data))

        return data
379175
def wr_tsv(self, fout_tsv):
        """"""
        with open(fout_tsv, 'w') as prt:
            kws_tsv = {
                'fld2fmt': {f:'{:8.2e}' for f in self.flds_cur if f[:2] == 'p_'},
                'prt_flds':self.flds_cur}
            prt_tsv_sections(prt, self.desc2nts['sections'], **kws_tsv)
            print("  WROTE: {TSV}".format(TSV=fout_tsv))
552253
def rst_to_json(text):
    
    records = []
    last_type = None
    key = None
    data = {}
    directive = False

    lines = text.splitlines()
    for index, line in enumerate(lines):

        # check for directives
        if len(line) and line.strip().startswith(".."):
            directive = True
            continue

        # set the title
        if len(line) and (line[0] in string.ascii_letters or line[0].isdigit()):
            directive = False
            try:
                if lines[index + 1][0] not in DIVIDERS:
                    continue
            except IndexError:
                continue
            data = text_cleanup(data, key, last_type)
            data = {"title": line.strip()}
            records.append(
                data
            )
            continue

        # Grab standard fields (int, string, float)
        if len(line) and line[0].startswith(":"):
            data = text_cleanup(data, key, last_type)
            index = line.index(":", 1)
            key = line[1:index]
            value = line[index + 1:].strip()
            data[key], last_type = type_converter(value)
            directive = False
            continue

        # Work on multi-line strings
        if len(line) and line[0].startswith(" ") and directive == False:
            if not isinstance(data[key], str):
                # Not a string so continue on
                continue
            value = line.strip()
            if not len(value):
                # empty string, continue on
                continue
            # add next line
            data[key] += "\n{}".format(value)
            continue

        if last_type == STRING_TYPE and not len(line):
            if key in data.keys():
                data[key] += "\n"

    return json.dumps(records)
709057
def serialize_for_header(key, value):
    """"""
    if key in QUOTE_FIELDS:
        return json.dumps(value)
    elif isinstance(value, str):
        if " " in value or "\t" in value:
            return json.dumps(value)
        else:
            return value
    elif isinstance(value, list):
        return "[{}]".format(", ".join(value))
    else:
        return str(value)
156597
def _climlab_to_rrtm_sfc(field, Ts):
    ''''''
    fieldsqueeze = np.squeeze(field)
    Tsqueeze = np.squeeze(Ts)
    return fieldsqueeze * np.ones_like(Tsqueeze)