Skip to content

Instantly share code, notes, and snippets.

@urbanij
Last active March 10, 2022 13:37
Show Gist options
  • Save urbanij/3b295f72e191958c533683e798437a53 to your computer and use it in GitHub Desktop.
Save urbanij/3b295f72e191958c533683e798437a53 to your computer and use it in GitHub Desktop.
tabs to spaces
#!/usr/bin/env python
"""
convert tabs to spaces and also prune 0xa0 characters, if any.
https://gist.github.com/urbanij/3b295f72e191958c533683e798437a53
Sat Nov 13 18:13:11 CET 2021
----
Fri Mar 4 09:53:51 CET 2022
also remove trailing whitespaces:
"""
import unicodedata
import sys
import pathlib
import re
def main(backup=False):
try:
filename = sys.argv[1]
except Exception as e:
print("Missing file. `./tabstospaces.py <file>`")
exit(1)
# reads
with open(filename, 'r') as f:
content = f.read()
# writes a backup
if backup:
with open(filename+'.bak', 'w') as f:
f.write(content)
# do the work
content = content.replace('\t', ' '*4)
content = unicodedata.normalize("NFKD", content) # strips 0xa0 from the string
TRAILING_WHITESPACES = r"( +)\n"
content = re.sub(TRAILING_WHITESPACES, "\n", content)
# writes new file
with open(filename, 'w') as f:
f.write(content)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment