print("File 'awesome_table.md' does not exist, for some reason.")
print("Here is some table:")
print(table)
```
Now, we want to parse the data:
1) Firstly, extract field names from the table header
```{python}
table_lines = table.split("\n") # split by lines
# Extract field names
field_names = table_lines[0].strip("| ").split("|") # split by the vertical line
field_names = [name.strip() for name in field_names] # remove whitespace
print("Field names:", field_names)
```
2) Then, extract data from each row and put these as a separate "record" (dictionary) into a list. Each value for a field is separated by a vertical line (pipe). However, a field might have multiple values, separated by comma. We want to split those and store them in a list.
```{python}
records = []
for line in table_lines[2:]: # skip the header and the separator
split_clean_line = line.strip("| ").split("|")
if len(split_clean_line) < len(field_names):
continue
print(f"Line: {split_clean_line}", line)
record = {}
for i, value in enumerate(split_clean_line):
values = value.strip().split(",")
if len(values) > 1:
record[field_names[i]] = [v.strip() for v in values]
else:
record[field_names[i]] = values[0]
records.append(record)
```
3) Finally, print the data:
```{python}
for ri, record in enumerate(records):
print(f"Record {ri}:")
for field, value in record.items():
print(f"\t{field:<17}: {str(value)}")
```
Be careful when splitting text by a character. Sometimes,
the same character might a be a part of text, e.g.:
```{python}
# split by comma but only if there is a space between the comma and the next word