leforestier / yattag

Python library to generate HTML or XML in a readable, concise and pythonic way.
333 stars 31 forks source link

Dealing with Null values #67

Open klayala opened 4 years ago

klayala commented 4 years ago

Hi.

I am a beginner using mostly python. I am currently trying to incorporate yattag. Currently I am using python to read in an excel file, however there are columns that do have null values and that is normal, because there should be missing values when other columns are not null.

Ideally, I would like for yattag if it sees that a value is NULL to output as

xsi:nil. here is the code template I am using `from openpyxl import load_workbook from yattag import Doc, indent wb = load_workbook("NY_baby_names.xlsx") ws = wb.worksheets[0] # Create Yattag doc, tag and text objects doc, tag, text = Doc().tagtext() xml_header = '' xml_schema = '' doc.asis(xml_header) doc.asis(xml_schema) with tag('Babies'): # Use ws.max_row for all rows for row in ws.iter_rows(min_row=2, max_row=100, min_col=1, max_col=5): row = [cell.value for cell in row] with tag("Baby"): with tag("Name"): text(row[2]) with tag("Gender"): text(row[1]) with tag("year"): text(row[0]) with tag("count"): text(row[3]) with tag("rank"): text(row[4]) result = indent( doc.getvalue(), indentation = ' ', indent_text = True ) with open("baby_names.xml", "w") as f: f.write(result)` Would this be possible? I would want for every row to print out in the xml form with all columns included, as opposed to not showing that column if it is null for that row.