Generate html from plain text with formatting markers in Python 3 -
Generate html from plain text with formatting markers in Python 3 -
i have written set of python 3 scripts take formatted text file , move info sqlite database. info in database used part of php application. info in text file has formatting markers bold , italics, not in intelligible browser. formatting scheme this:
fi:xxxx (italics on word xxxx (turned off @ word break)) fi:{xxx…xxx} (italics on word or phrase in curly brackets {}) fb:xxxx (bold on word xxxx (turned off @ word break)) fb:{xxx} (bold on word or phrase in brackets {}) fv:xxxx (bold on word xxxx (turned off @ word break)) fv:{xxx…xxx} (bold on word or phrase in brackets {}) fn:{xxx…xxx} (no formatting)
i convert each line of source text (1. line containing string, using html tags instead of source formatting , 2. line, containing string stripped of formatting markers). need formatted , stripped line each source line, if no formatting markers used on line. in source data, multiple formatting markers of different (or same) sort may show in single line, won't find marker doesn't end before line does.
to format bracketed sections, this:
while text.find(":{") > -1: index = text.find(":{") if text[index-2:index]=="fb": text = text[:index-2] + "<b>" + text[index+2:] #insert <b> text = text.replace("}","</b>",1) # replace one. # else if fi, fv, etc.
this convert "other fb:{bold text} text" "other bold text text".
then convert space-separated sections:
array = text.split(" ") word in array: if (word.startswith("fi")): word = "<i>"+word[2:]+"</i>" else if (word.startswith("fb")): .... text = " ".join(array)
if want plain text replace tags such "<b>" , "</b>" empty string "".
if formatting doesn't span multiple lines improve performance reading , converting line line with:
infile = open("file.txt","r") outfile = open("file.out","w") def convert(text): #change text here. homecoming text line in infile: outfile.write(convert(line))
python html python-3.x text-parsing
Comments
Post a Comment