-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actual modifications that belong with commit 4300aa1
Fixes Unicode handling in BibTexParser and charset detection and BOM removal in BaseParser. NOTE: This has the limitation that input files need to fit in memory, as the input file is read and then provided to Subclasses via StringIO.
- Loading branch information
Showing
3 changed files
with
20 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import cStringIO | ||
import chardet | ||
|
||
class BaseParser(object): | ||
def __init__(self, fileobj): | ||
if hasattr(fileobj, 'seek'): | ||
# Some files have Byte-order marks inserted at the start | ||
possible_BOM = fileobj.read(3) | ||
if possible_BOM != '\xef\xbb\xbf': | ||
fileobj.seek(0) | ||
self.fileobj = fileobj | ||
data = fileobj.read() | ||
self.encoding = chardet.detect(data).get('encoding', 'ascii') | ||
|
||
# Some files have Byte-order marks inserted at the start | ||
if data[:3] == '\xef\xbb\xbf': | ||
data = data[3:] | ||
self.fileobj = cStringIO.StringIO(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters