XtinaS
XtinaS

Reputation: 194

Handle a file-ending non-printable ASCII character in Python

Steps:

The problem:

Somehow, I don't know how, one row in the DBF file converts out the ASCII non-printable character SUB. Python treats this as the end of the file, and so it freaks out at me that I haven't properly terminated the string.

How would I, on a Windows machine, replace all ASCII symbols with anything else, without Python flipping out?

Upvotes: 0

Views: 491

Answers (1)

tdelaney
tdelaney

Reputation: 77387

You can replace non-printable symbols using the re module, but that may not solve the problem you have here. DBF is a binary file format and there are lots of non-ascii characters in it.

import re
ascii_sym_re = re.compile(r'[\x00-\x09\x0b-\x1f\x7f-\xff]', re.DOTALL|re.MULTILINE)
print ascii_sym_re.sub('.', 'hello\x1athere')
with open('somefile','rb') as fp_in, open('somefile-scrubbed', 'wb') as fp_out:
    fp_out.write(ascii_sym_re.sub('.', fp_in.read())

Upvotes: 2

Related Questions