def prefix_by_topic(topic_str):
    """
    Return a topic-based prefix to produce distinct output files.

    `+` in the topic is replaced with `ALL`.
    The last element, `#` is left out from the prefix.
    See https://digitransit.fi/en/developers/apis/4-realtime-api/vehicle-positions/#the-topic
    """
    els = topic_str.split('/')[1:]
    els = [el for el in els[:-1] if el is not None]
    els = [el.replace('+', 'ALL') for el in els]
    return '_'.join(els)