Convert Text To HTML Char Codes

I got tired of hand-formatting or shell-scripting a text converter for this blog to post code properly so I wrote this py script to do it for me. It converts a text file containing possibly used HTML characters into HTML character codes.

#!/usr/bin/python
import sys
s = ""
l = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
while (1):
	c = sys.stdin.read(1)
	if (not c):
		break
	o = ord(c)
	if (o == 13):
		continue
	elif ((o < 127) and (c not in l)):
		s += ("&#" + str(o) + ";")
	else:
		s += (c)
print(s)

3 thoughts on “Convert Text To HTML Char Codes

Leave a comment