Skip to content
Snippets Groups Projects
Commit 5c6aae6e authored by Chris Smith's avatar Chris Smith
Browse files

Updated parse_dm3 for Python3

Updated the parse_dm3 file.
The StringIO imports should now work on both python 2 and 3.
The objects read from the binary file are now decoded as utf-8 when possible.  If decoding fails, the error is ignored and the file read procedes without crashing.
parent 4098d515
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,7 @@ import array
import warnings
import re
try:
import StringIO
import StringIO.StringIO
except ImportError:
from io import StringIO
unicode = str
......@@ -35,6 +35,9 @@ def get_from_file(f, stype):
src = f.read(struct.calcsize(stype))
assert(len(src) == struct.calcsize(stype))
d = struct.unpack(stype, src)
if verbose:
print(d)
d = [d1.decode('utf-8', 'ignore') if isinstance(d1, bytes) else d1 for d1 in d]
if len(d) == 1:
return d[0]
else:
......@@ -45,8 +48,8 @@ def put_into_file(f, stype, *args):
f.write(struct.pack(
stype, *args))
read_array = lambda f, a, l: a.fromstring(f.read(l*struct.calcsize(a.typecode))) if isinstance(f, StringIO.StringIO) else a.fromfile(f, l)
write_array = lambda f, a: f.write(a.tostring()) if isinstance(f, StringIO.StringIO) else a.tofile(f)
read_array = lambda f, a, l: a.fromstring(f.read(l*struct.calcsize(a.typecode))) if isinstance(f, StringIO) else a.fromfile(f, l)
write_array = lambda f, a: f.write(a.tostring()) if isinstance(f, StringIO) else a.tofile(f)
class structarray(object):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment