Skip to content

Instantly share code, notes, and snippets.

@AndrewOwenMartin
Created September 9, 2020 10:50
Show Gist options
  • Save AndrewOwenMartin/906a3eefb9fa27fc0353b40a444fe678 to your computer and use it in GitHub Desktop.
Save AndrewOwenMartin/906a3eefb9fa27fc0353b40a444fe678 to your computer and use it in GitHub Desktop.
A reader for the concordance DAT file format
import collections
import logging, pathlib
import csv
log = logging.getLogger(__name__)
def read_dat(path):
with path.open("r", encoding="latin1") as f:
csv_reader = csv.reader(f, delimiter="\x14", quotechar="\xfe")
header_row = list(next(csv_reader))
DatRow = collections.namedtuple("DatRow", header_row)
yield from (DatRow(*row) for row in csv_reader)
def main():
path = pathlib.Path("example.dat")
row_gen = read_dat(path)
for num, row in enumerate(row_gen):
log.info("%s: %s", num, row)
if num > 10:
break
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
format="%(asctime)s %(levelname)-4s %(name)s %(message)s",
style="%",
)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment