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)

Advertisements
Convert Text To HTML Char Codes

2 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s