Skip to content

Instantly share code, notes, and snippets.

@jerrylususu
Last active March 30, 2018 17:25
Show Gist options
  • Save jerrylususu/d39ae34ab20852e779f5ea428dcc23de to your computer and use it in GitHub Desktop.
Save jerrylususu/d39ae34ab20852e779f5ea428dcc23de to your computer and use it in GitHub Desktop.
comment.txt to html
# txt2html
import os
import chardet
s = os.sep
dirname = "C:\Projects\Assignment1"
# your assignment dir goes here
for root, dirs, files in os.walk(dirname):
for file in files:
if(file=="comments.txt"):
print(os.path.join(root, file))
f = open(os.path.join(root, file),'rb')
data = f.read()
f.close()
guess_encoding = chardet.detect(data)['encoding']
print(guess_encoding)
f = open(os.path.join(root, file),'r',encoding=guess_encoding)
content = f.read()
lines = content.splitlines()
print(lines)
f.close()
f2 = open(os.path.join(root, "feedbackText.html"),'w',encoding="utf-8")
for line in lines:
line = "<p>{{" + line +"}}</p>"
f2.writelines(line)
f2.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment