Skip to content

Instantly share code, notes, and snippets.

@hcs42
Last active August 29, 2015 14:20
Show Gist options
  • Save hcs42/c154d5602add5ccf1751 to your computer and use it in GitHub Desktop.
Save hcs42/c154d5602add5ccf1751 to your computer and use it in GitHub Desktop.
remove_nbsp: script that replaces non-breaking space charaters
#!/usr/bin/env python3
# This script replaces each non-breaking space charaters with a space character
# in the given files.
#
# Usage: remove_nbsp <files>
#
# Examples:
#
# Replace NBSPs in two files:
#
# $ remove_nbsp file1.txt file2.txt
#
# Replace NBSPs in each Erlang source file and header file that is
# (recursively) inside the 'apps' directory:
#
# $ remove_nbsp $(git ls-files 'apps/*.[eh]rl')
# File fixed: apps/wo_plugins/include/wo_plugins.hrl
import re
import sys
if len(sys.argv) == 1:
print('Usage: remove_nbsp <files>')
sys.exit(1)
NBSP = '\u00a0' # char code for non-breaking space
for fname in sys.argv[1:]:
with open(fname) as f:
try:
contents = f.read()
if NBSP not in contents:
continue
except Exception:
print('Warning: Cannot read file:', fname)
continue
with open(fname, 'w') as f:
contents = re.sub(NBSP, ' ', contents)
f.write(contents)
print('File fixed:', fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment