Skip to content

Instantly share code, notes, and snippets.

@dmsnell
Last active July 31, 2024 19:31
Show Gist options
  • Save dmsnell/febbe9a3d9bd265af5bc4564dcbe1f86 to your computer and use it in GitHub Desktop.
Save dmsnell/febbe9a3d9bd265af5bc4564dcbe1f86 to your computer and use it in GitHub Desktop.
Prepend the BOM to indicate a file is UTF-8
import codecs
import sys
def prepend_bom(filename):
with open( filename, 'r+b' ) as csv:
existing = csv.read()
if b'\xef\xbb\xbf' == existing[0:3]:
print("Byte-order mark already present: leaving file as-is.")
return
print("Adding byte-order mark to beginning of file to indicate that it's a UTF-8 file.")
csv.seek(0)
csv.write(codecs.BOM_UTF8)
csv.write(existing)
if __name__ == '__main__':
if len(sys.argv) < 2:
print(f"Supply a filename: python3 {sys.argv[0]} survey-results.csv")
sys.exit(1)
filename = sys.argv[1]
prepend_bom(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment