Python Example
Get text chunks
Get all the labels that match the document.
import json import requests api_host = 'https://api.lettria.com' api_route = api_host + '/parse' document_file_name = 'file.html' document_file_location = './data/' jwt_token_file = 'jwt_token.txt' jwt_token = open(jwt_token_file, 'r').read() chunks = [] with open(document_file_location + document_file_name, 'rb') as file: response = requests.post( api_route, files=[('file', ( document_file_name, file))], headers={"Authorization":"LettriaProKey " + jwt_token} ).json() chunks = response['chunks'] text_chunks = [ chunk for chunk in chunks if chunk['type'] == 'text' ] print(json.dumps(text_chunks, indent=4, sort_keys=True)) text_content = '\n'.join([ chunk['content'] for chunk in text_chunks ]) print(text_content)
In this code, the document is sent to Lettria for parsing. ON it's return, text chunks are extracted and printed.
Next steps
xxxxxxxxxx
1
45
---
title: "Python: Parse – Lettria API"
description: Examples of various different requests to the Lettria API classification end point using different programming languages, to demonstrate how requests are made.
---
# Python Example
## Get text chunks
Get all the labels that match the document.
```py
import json
import requests
api_host = 'https://api.lettria.com'
api_route = api_host + '/parse'
document_file_name = 'file.html'
document_file_location = './data/'
jwt_token_file = 'jwt_token.txt'
jwt_token = open(jwt_token_file, 'r').read()
chunks = []
with open(document_file_location + document_file_name, 'rb') as file:
response = requests.post(
api_route,
files=[('file', ( document_file_name, file))],
headers={"Authorization":"LettriaProKey " + jwt_token}
).json()
chunks = response['chunks']
text_chunks = [ chunk for chunk in chunks if chunk['type'] == 'text' ]
print(json.dumps(text_chunks, indent=4, sort_keys=True))
text_content = '\n'.join([ chunk['content'] for chunk in text_chunks ])
print(text_content)
```
In this code, the document is sent to Lettria for parsing. ON it's return, text chunks are extracted and printed.
## Next steps
- [Learn API Format](/api-reference/parse/2.0/schemas/document)