Unstructuredunstructured 0.27.6 · Python 3.11+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
29 small wins to finish your pathNext lesson

Text, markdown and a dataframe

JSON suits a program that reads elements back. The same list can also become plain text, markdown for a language model, or a pandas dataframe for counting and filtering.

Plain text

Example
from unstructured.partition.auto import partition
from unstructured.staging.base import elements_to_text

print(elements_to_text(partition("refunds.html")))

One element per line. The table from lesson 9 became a single line of values, and nothing says which number belongs to which column.

Markdown

Example
from unstructured.partition.auto import partition
from unstructured.staging.base import elements_to_md

print(elements_to_md(partition("refunds.html")))

Two things changed. The table kept its rows and columns, because elements_to_md writes a table's text_as_html into the markdown. The headings lost their levels: the page's h1 and both h2 headings all came out as #.

Example
from unstructured.partition.auto import partition

for element in partition("refunds.html"):
    if element.category == "Title":
        print(element.metadata.category_depth, element.text)

The level is still in the metadata as category_depth. Markdown written by elements_to_md is fine for a model to read, but a program that needs the outline should use the elements, not the markdown.

A pandas dataframe

Example
from unstructured.partition.auto import partition
from unstructured.staging.base import convert_to_dataframe

frame = convert_to_dataframe(partition("refunds.html"))
print(frame[["type", "text"]])
print(frame["type"].value_counts().to_dict())

One row per element and one column per field. Metadata fields become columns of their own, and an element without a value for a field gets an empty cell. Counting elements by type, or finding every table across a folder, is one line of pandas.

Try it yourself
  • Print frame.columns and find the metadata fields that became columns.
  • Run elements_to_md on shipping.md and compare the headings with the original file.
  • Filter the dataframe to the rows whose type is Table and print text_as_html.

Slow is fine. Stopping is the only problem.