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)

Convert Text To HTML Char Codes

3 thoughts on “Convert Text To HTML Char Codes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s