<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
Step1:  The object of this exercise is to find the UUIDs for processes dealing with sugar beet production.
The plan is
Step2:  11 beet-related flows
Step3:  that didn't work... it's because 'beets' is a list of flow objects, but exchange entries only list references to flow objects
Step4:  This can include duplicates... set forces the entries to be unique (set members)
Step5:  use pandas to print the processes as a table
Step6:  p_list is a list of full process records -- we want to view all the semantic content of these records -- all the tags
Step7:  Write this info to a CSV file in the current directory
<ASSISTANT_TASK:>
Python Code:
import json
import re
with open('../catalogs/json/ecoinvent_3.2_undefined_xlsx.json') as fp:
    ei32 = json.load(fp)
def search_tags(entity, search):
    
    This function searches through all the 'tags' (semantic content) of a data set
    and returns 'true' if the search expression is found. case insensitive.
    
    all_tags = '; '.join([str(x) for x in entity['tags'].values()])
    return bool(re.search(search, all_tags, flags=re.IGNORECASE))
beets = [flow for flow in ei32['flows'] if search_tags(flow,'beet')]
len(beets)
Explanation: The object of this exercise is to find the UUIDs for processes dealing with sugar beet production.
The plan is:
 1. find flows that deal with 'beets'
 2. collect processes mentioned in exchanges that involve those flows
End of explanation
[b['tags']['Name'] for b in beets]
beet_processes = [x['process'] for x in ei32['exchanges'] if x['flow'] in beets]
len(beet_processes)
Explanation: 11 beet-related flows
End of explanation
beet_refs = [b['dataSetReference'] for b in beets]
beet_processes = [x['process'] for x in ei32['exchanges'] if x['flow'] in beet_refs]
len(beet_processes)
Explanation: that didn't work... it's because 'beets' is a list of flow objects, but exchange entries only list references to flow objects
End of explanation
len(set(beet_processes))
[p for p in ei32['processes'] if p['dataSetReference'] == beet_processes[0]]
Explanation: This can include duplicates... set forces the entries to be unique (set members)
End of explanation
import pandas as pd
p_list = [p for p in ei32['processes'] if p['dataSetReference'] in set(beet_processes)]
Explanation: use pandas to print the processes as a table
End of explanation
P = pd.DataFrame([p['tags'] for p in p_list], 
                 index=[p['dataSetReference'] for p in p_list])
P
Explanation: p_list is a list of full process records -- we want to view all the semantic content of these records -- all the tags
End of explanation
P.to_csv('beet_processes.csv')
Explanation: Write this info to a CSV file in the current directory
End of explanation
<END_TASK>