we cannot use str split(","to split each line into fields because commas can occur inside quoted strings so we have farmed this work out to the extract_fields(function once we have list of the fields (as stringswith no surrounding quotes)we iterate over themcreating table cell for each one if field is emptywe output an empty cell if field is quotedit could be string or it could be number that has been quoted to allow for internal commasfor example" , to account for thiswe make copy of the field with commas removed and try to convert the field to float if the conversion is successful we output right-aligned cell with the field rounded to the nearest whole number and output it as an integer if the conversion fails we output the field as string in this case we use str title(to neaten the case of the letters and we replace the word and with and as correction to str title()' effect if the field isn' too long we use all of itotherwise we truncate it to maxwidth characters and add an ellipsis to signify the truncationand in either case we escape any special html characters the field might contain def extract_fields(line)fields [field "quote none for in lineif in "\"'"if quote is nonestart of quoted string quote elif quote =cend of quoted string quote none elsefield + other quote inside quoted string continue if quote is none and =","end of field fields append(fieldfield "elsefield + accumulating field if fieldfields append(fieldadding the last field return fields this function reads the line it is given character by characteraccumulating list of fields--each one string without any enclosing quotes the function copes with fields that are unquotedand with fields that are quoted with single or double quotesand correctly handles commas and quotes (single quotes in double quoted stringsdouble quotes in single quoted strings