# 🐍 Examples and Code Snippets

Source: https://help.airparser.com/post-processing/examples-and-code-snippets

### Prevent a document from being exported

If you don't need to export some of the parsed documents (typically, based on a certain condition), simply return None. This will mark the document as "Skipped." Skipped documents aren't exported to Webhooks, Zapier, and other integrations.

```python
return None
```

Here's an example of how to prevent emails from being exported to your webhooks and integrations when you only need to export parsed attachments.

To do this, enable the "Content Type" meta field on the Inbox Settings page.

```python
if "_content_type_" in data and data["_content_type_"] == "message/rfc822":
    return None
```

### Create a new field

```python
# Create field
data['new_field'] = 'Field value'

# Create field based on another field. If 'email' doesn't exist, use the default value
data['customer_email'] = data.get("email", 'default@example.com')
```

### Delete a field

```python
del data['email']
```

### Merge multiple fields into one

```python
# Using string concatenation (+ operator)
data['fullname'] = data['first_name'] + " " + data['last_name']

# Using formatted string literals (f-strings)
data["fullname"] = f"{data['first_name']} {data['last_name']}"

# Using format() function
data['fullname'] = '{} {}'.format(data['first_name'], data['last_name'])
```

### Rename a field

This code will remove the original `email` field and insert a new field `customer_email` with the same value:

```python
data['customer_email'] = data.pop('email')
```

### Check if field exists in parsed data

```python
# If 'name' exists, change its value
if "name" in data:
	data['name'] = 'Peter'

# If 'name' doesn't exists, create field
if "name" not in data:
	data['name'] = 'Peter'
# ...or in one line
data['name'] = data.get('name', 'Peter')
```

### Store parsed data in another variable

You can also create a new dictionary which will replace the original `data` dictionary.

```python
parsed_data = {
	**data,		# copy all the fields from the 'data' dictinary
	'name': 'Peter',
	'age': 27
}
return parsed_data    # do not forget to 'return' the dictionary
```

### Round a floating point number

```python
pi = 3.141592653589793
data['pi'] = round(pi, 2) # 3.14
```

### Formatting dates

Airparser provides 2 Python modules to work with dates: [datetime](https://docs.python.org/3/library/datetime.html) and [dateparser](https://dateparser.readthedocs.io/en/latest/).

```python
date = data['date'] 	# "Mon, Mar 28, 2022"
format_out = "%Y-%m-%d" # YYYY-MM-DD

# Using the 'datetime' module
format_in = "%a, %b %d, %Y"
date_out_1 = datetime.strptime(date, format_in).strftime(format_out)
data['date_formatted_1'] = date_out_1

# Using the 'dateparser' module
date_parsed = dateparser.parse(date)
date_out_2 = date_parsed.strftime(format_out)
data['date_formatted_2'] = date_out_2
```

![](https://cdn.eniston.com/bases/539620d4-93af-413a-b051-73249e9322a9/attachments/shot-2024-05-10-at-14-14-06-2x-1715343677-355_1732566277.jpg)
