[Python-checkins] python/nondist/sandbox/csv/util sniffer.py,NONE,1.1
cliffwells18@users.sourceforge.net
cliffwells18@users.sourceforge.net
2003年3月12日 10:58:26 -0800
Update of /cvsroot/python/python/nondist/sandbox/csv/util
In directory sc8-pr-cvs1:/tmp/cvs-serv21719
Added Files:
sniffer.py
Log Message:
Initial commit of sniffer.py
--- NEW FILE: sniffer.py ---
"""
dialect = Sniffer().sniff(file('csv/easy.csv'))
print "delimiter", dialect.delimiter
print "quotechar", dialect.quotechar
print "skipinitialspace", dialect.skipinitialspace
"""
from csv import csv
import re, string
class Sniffer:
"""
"Sniffs" the format of a CSV file (i.e. delimiter, quotechar)
Sniffer.dialect will be either a csv.Dialect object or None
if the file format couldn't be determined.
"""
def __init__(self, sample = 16 * 1024):
# in case there is more than one possible delimiter
self.preferred = [',', '\t', ';', ' ', ':']
# amount of data (in bytes) to sample
self.sample = sample
def sniff(self, fileobj):
"""
Takes a file-like object and returns a dialect (or None)
"""
data = fileobj.read(self.sample)
quotechar, delimiter, skipinitialspace = self._guessQuoteAndDelimiter(data)
if delimiter is None:
delimiter, skipinitialspace = self._guessDelimiter(data)
print quotechar, delimiter, skipinitialspace
class Dialect(csv.Dialect):
_name = "sniffed"
lineterminator = '\r\n'
quoting = csv.QUOTE_MINIMAL
escapechar = ''
doublequote = False
Dialect.delimiter = delimiter
Dialect.quotechar = quotechar
Dialect.skipinitialspace = skipinitialspace
return Dialect()
def _guessQuoteAndDelimiter(self, data):
"""
Looks for text enclosed between two identical quotes
(the probable quotechar) which are preceded and followed
by the same character (the probable delimiter).
For example:
,'some text',
The quote with the most wins, same with the delimiter.
If there is no quotechar the delimiter can't be determined
this way.
"""
for restr in ('(?P<delim>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?P=delim)', # ,".*?",
'(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?P<delim>[^\w\n"\'])(?P<space> ?)', # ".*?",
'(?P<delim>>[^\w\n"\'])(?P<space> ?)(?P<quote>["\']).*?(?P=quote)(?:$|\n)', # ,".*?"
'(?:^|\n)(?P<quote>["\']).*?(?P=quote)(?:$|\n)'): # ".*?" (no delim, no space)
regexp = re.compile(restr, re.S | re.M)
matches = regexp.findall(data)
if matches:
print restr
print matches
break
if not matches:
return ('', None, 0) # (quotechar, delimiter, skipinitialspace)
quotes = {}
delims = {}
spaces = 0
for m in matches:
n = regexp.groupindex['quote'] - 1
key = m[n]
if key:
quotes[key] = quotes.get(key, 0) + 1
try:
n = regexp.groupindex['delim'] - 1
key = m[n]
except KeyError:
continue
if key:
delims[key] = delims.get(key, 0) + 1
try:
n = regexp.groupindex['space'] - 1
except KeyError:
continue
if m[n]:
spaces += 1
print "QUOTES", quotes
print "DELIMS", delims
quotechar = reduce(lambda a, b, quotes = quotes:
(quotes[a] > quotes[b]) and a or b, quotes.keys())
if delims:
delim = reduce(lambda a, b, delims = delims:
(delims[a] > delims[b]) and a or b, delims.keys())
skipinitialspace = delims[delim] == spaces
if delim == '\n': # most likely a file with a single column
delim = None
else:
# there is *no* delimiter, it's a single column of quoted data
delim = ''
skipinitialspace = 0
return (quotechar, delim, skipinitialspace)
def _guessDelimiter(self, data):
"""
The delimiter /should/ occur the same number of times on
each row. However, due to malformed data, it may not. We don't want
an all or nothing approach, so we allow for small variations in this
number.
1) build a table of the frequency of each character on every line.
2) build a table of freqencies of this frequency (meta-frequency?),
e.g. "x occurred 5 times in 10 rows, 6 times in 1000 rows,
7 times in 2 rows"
3) use the mode of the meta-frequency to determine the /expected/
frequency for that character
4) find out how often the character actually meets that goal
5) the character that best meets its goal is the delimiter
For performance reasons, the data is evaluated in chunks, so it can
try and evaluate the smallest portion of the data possible, evaluating
additional chunks as necessary.
"""
data = filter(None, data.split('\n'))
ascii = [chr(c) for c in range(127)] # 7-bit ASCII
# build frequency tables
chunkLength = min(10, len(data))
iteration = 0
charFrequency = {}
modes = {}
delims = {}
start, end = 0, min(chunkLength, len(data))
while start < len(data):
iteration += 1
for line in data[start:end]:
for char in ascii:
metafrequency = charFrequency.get(char, {})
freq = line.strip().count(char) # must count even if frequency is 0
metafrequency[freq] = metafrequency.get(freq, 0) + 1 # value is the mode
charFrequency[char] = metafrequency
for char in charFrequency.keys():
items = charFrequency[char].items()
if len(items) == 1 and items[0][0] == 0:
continue
# get the mode of the frequencies
if len(items) > 1:
modes[char] = reduce(lambda a, b: a[1] > b[1] and a or b, items)
# adjust the mode - subtract the sum of all other frequencies
items.remove(modes[char])
modes[char] = (modes[char][0], modes[char][1]
- reduce(lambda a, b: (0, a[1] + b[1]), items)[1])
else:
modes[char] = items[0]
# build a list of possible delimiters
modeList = modes.items()
total = float(chunkLength * iteration)
consistency = 1.0 # (rows of consistent data) / (number of rows) = 100%
threshold = 0.9 # minimum consistency threshold
while len(delims) == 0 and consistency >= threshold:
for k, v in modeList:
if v[0] > 0 and v[1] > 0:
if (v[1]/total) >= consistency:
delims[k] = v
consistency -= 0.01
if len(delims) == 1:
delim = delims.keys()[0]
skipinitialspace = data[0].count(delim) == data[0].count("%c " % delim)
return (delim, skipinitialspace)
# analyze another chunkLength lines
start = end
end += chunkLength
if not delims:
return None
# if there's more than one, fall back to a 'preferred' list
if len(delims) > 1:
for d in self.preferred:
if d in delims.keys():
skipinitialspace = data[0].count(d) == data[0].count("%c " % d)
return (d, skipinitialspace)
# finally, just return the first damn character in the list
delim = delims.keys()[0]
skipinitialspace = data[0].count(delim) == data[0].count("%c " % delim)
return (delim, skipinitialspace)