def inferdialect(fname=None, datalines=None, delimiter_regex=None, <EOL>verbosity=DEFAULT_VERBOSITY):
if datalines is None:<EOL><INDENT>if is_string_like(fname):<EOL><INDENT>fh = file(fname, '<STR_LIT>')<EOL><DEDENT>elif hasattr(fname, '<STR_LIT>'):<EOL><INDENT>fh = fname<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>datalines = fh.read().strip().split('<STR_LIT:\n>')<EOL>fh.close() <EOL><DEDENT>if delimiter_regex:<EOL><INDENT>matches = []<EOL>for l in datalines[:<NUM_LIT:10>]:<EOL><INDENT>matches += delimiter_regex.findall(l)<EOL><DEDENT>poss = {}<EOL>for m in matches:<EOL><INDENT>for x in set(m):<EOL><INDENT>poss[x] = m.count(x) + (poss[x] if x in list(poss.keys()) else <NUM_LIT:0>) <EOL><DEDENT><DEDENT>MaxVal = max(poss.values())<EOL>assert MaxVal > <NUM_LIT:0>, '<STR_LIT>'<EOL>amax = [x for x in list(poss.keys()) if poss[x] == MaxVal][<NUM_LIT:0>]<EOL>return csv.Sniffer().sniff(amax)<EOL><DEDENT>else:<EOL><INDENT>if not is_string_like(fname):<EOL><INDENT>fname = None<EOL><DEDENT>tries = [<NUM_LIT:10>, <NUM_LIT:30>, <NUM_LIT>, <NUM_LIT:100>, <NUM_LIT:200>, <NUM_LIT>, <NUM_LIT>]<EOL>if len(datalines) > <NUM_LIT:100>:<EOL><INDENT>starts = [int(len(datalines) / <NUM_LIT:5>) * i for i in range(<NUM_LIT:5>)]<EOL><DEDENT>else:<EOL><INDENT>starts = [<NUM_LIT:0>, int(len(datalines) / <NUM_LIT:2>)]<EOL><DEDENT>G = []<EOL>for s in starts:<EOL><INDENT>for t in [tt for (i, tt) in enumerate(tries) <EOL>if i == <NUM_LIT:0> or s + tries[i-<NUM_LIT:1>] <= len(datalines)]:<EOL><INDENT>try:<EOL><INDENT>g = csv.Sniffer().sniff('<STR_LIT:\n>'.join(datalines[s:(s+t)]))<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>G += [g]<EOL>break<EOL><DEDENT><DEDENT><DEDENT>delims = [g.delimiter for g in G]   <EOL>G = [g for (i, g) in enumerate(G) if g.delimiter not in delims[:i]]<EOL>V = []<EOL>for g in G:<EOL><INDENT>lvec = np.array([len(r) for r in <EOL>list(csv.reader(datalines[:<NUM_LIT:1000>], dialect=g))])  <EOL>V += [lvec.var()]<EOL><DEDENT>if len(G) > <NUM_LIT:0>:<EOL><INDENT>V = np.array(V)<EOL>if V.min() > <NUM_LIT:0>:<EOL><INDENT>fnamedelim = inferdelimiterfromname(fname)<EOL>if fnamedelim not in delims:<EOL><INDENT>fnamevar = np.array([len(r) for r in <EOL>list(csv.reader(datalines[:<NUM_LIT:1000>], <EOL>delimiter=fnamedelim))]).var()<EOL>if fnamevar < V.min():<EOL><INDENT>return csv.Sniffer().sniff(fnamedelim)<EOL><DEDENT><DEDENT><DEDENT>return G[V.argmin()]<EOL><DEDENT>else:<EOL><INDENT>if verbosity > <NUM_LIT:2>:<EOL><INDENT>print('<STR_LIT>', inferdelimiterfromname(fname) , '<STR_LIT>')<EOL><DEDENT>return csv.Sniffer().sniff(inferdelimiterfromname(fname))<EOL><DEDENT><DEDENT>
Attempts to convert infer dialect from csv file lines. 

Essentially a small extension of the "sniff" function from Python CSV 
module.   csv.Sniffer().sniff attempts to infer the delimiter from a 
putative delimited text file by analyzing character frequencies.  This 
function adds additional analysis in which guesses are checked again the 
number of entries in each line that would result from splitting relative to 
that guess. If no plausable guess if found, delimiter is inferred from file 
name  ('csv' yields ',', everything else yields '\t'.)

**Parameters** 

    **fname** : pathstring

        Name of file.

    **datalines** : list of strings

        List of lines in the data file.

    **lineterminator** : single-character string

        Line terminator to join split/join line strings.

**Returns**

    csv.Dialect obejct