Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Raises
------
NoDetectionResult
When the dialect detection fails.
"""
if encoding is None:
encoding = get_encoding(filename)
with open(filename, "r", newline="", encoding=encoding) as fid:
if dialect is None:
data = fid.read(num_chars) if num_chars else fid.read()
dialect = Detector().detect(data, verbose=verbose)
if dialect is None:
raise NoDetectionResult()
fid.seek(0)
r = reader(fid, dialect)
yield from r
out = (
io.StringIO(newline=None)
if output is None
else open(output, "w", encoding=encoding)
)
if self.option("transpose"):
with open(path, "r", newline="", encoding=encoding) as fp:
read = reader(fp, dialect=dialect)
rows = list(read)
rows = list(map(list, zip(*rows)))
write = writer(out, dialect="excel")
for row in rows:
write.writerow(row)
else:
with open(path, "r", newline="", encoding=encoding) as fp:
read = reader(fp, dialect=dialect)
write = writer(out, dialect="excel")
for row in read:
write.writerow(row)
if output is None:
self.overwrite(out.getvalue())
out.close()
def __init__(
self,
f,
fieldnames=None,
restkey=None,
restval=None,
dialect="excel",
*args,
**kwds
):
self._fieldnames = fieldnames
self.restkey = restkey
self.restval = restval
self.reader = reader(f, dialect, *args, **kwds)
self.dialect = dialect
self.line_num = 0
path, num_chars=num_chars, encoding=encoding, verbose=verbose
)
except UnicodeDecodeError:
return self.line(WARNINGS["unicodedecodeerror"])
if dialect is None:
return self.line("Dialect detection failed.")
out = (
io.StringIO(newline=None)
if output is None
else open(output, "w", encoding=encoding)
)
if self.option("transpose"):
with open(path, "r", newline="", encoding=encoding) as fp:
read = reader(fp, dialect=dialect)
rows = list(read)
rows = list(map(list, zip(*rows)))
write = writer(out, dialect="excel")
for row in rows:
write.writerow(row)
else:
with open(path, "r", newline="", encoding=encoding) as fp:
read = reader(fp, dialect=dialect)
write = writer(out, dialect="excel")
for row in read:
write.writerow(row)
if output is None:
self.overwrite(out.getvalue())
out.close()
This function is copied from CPython! The only change we've made is to
use our dialect detection method.
"""
# Creates a dictionary of types of data in each column. If any
# column is of a single type (say, integers), *except* for the first
# row, then the first row is presumed to be labels. If the type
# can't be determined, it is assumed to be a string in which case
# the length of the string is the determining factor: if all of the
# rows except for the first are the same length, it's a header.
# Finally, a 'vote' is taken at the end for each column, adding or
# subtracting from the likelihood of the first row being a header.
rdr = reader(StringIO(sample), self.sniff(sample))
header = next(rdr) # assume first row is header
columns = len(header)
columnTypes = {}
for i in range(columns):
columnTypes[i] = None
checked = 0
for row in rdr:
# arbitrary number of rows to check, to keep it sane
if checked > 20:
break
checked += 1
if len(row) != columns: