1. Parse the file, process only the field as a key in the fields dictionary, and return the list of values in the Clean up dictionary
Demand:
1. Change the dictionary keys according to the mappings in the fields dictionary
2. Delete the extra instructions in the parentheses in "Rdf-schema#label", for example "(spider)"
3. If "name" is "NULL" or contains non-alphanumeric characters, set it to the same value as "label"
4. If the value of the field is "NULL", convert it to "None"
5. If a value exists in "synonym", it should be converted to an array (list) by deleting the "{}" character and splitting the string according to "|". The rest of the cleanup will be at your discretion, such as deleting the prefix "*" and so on. If there is a singular synonym, the value should remain in the list format.
6. Delete the spaces before and after all fields (if any)
7. The output structure should resemble the following
[ {'label':'Argiope', 'URI':'Http://dbpedia.org/resource/Argiope_ (spider)', 'Description':'The genus Argiope includes rather large and spectacular spiders that often ...', 'name':'Argiope', 'synonym': [" One"," Both"], 'Classification': { 'Family':'orb-weaver Spider', 'class':'arachnid', 'Phylum':'arthropod', 'Order':'Spider', 'Kingdom':'Animal', 'Genus': None}}, {'label': ... , }, ...]
ImportCodecsImportCSVImportJSONImportPprintImportRedatafile='Arachnid.csv' Fields={'Rdf-schema#label':'label', 'URI':'URI', 'rdf-schema#comment':'Description', 'synonym':'synonym', 'name':'name', 'Family_label':'Family', 'Class_label':'class', 'Phylum_label':'Phylum', 'Order_label':'Order', 'Kingdom_label':'Kingdom', 'Genus_label':'Genus'}defprocess_file (filename, fields): The keys list of the #获取 fields dictionary process_fields=Fields.keys ()
#存放结果集 Data=[] with open (filename,"R") as F:reader=CSV. Dictreader (f)
#跳过文件中的前3行 forIinchRange (3): L=reader.next () #读文件 forLineinchReader:#YOUR CODE here
#存放总的字典res = {}
#存放key是classification的子字典 res['Classification'] = {}
keys for #循环 fields Dictionary forFieldinchProcess_fields:
#获取excel中key所对应的val, condition 1 tmp_val=Line[field].strip ()
#生成json数据的新key, which is the value of the fields dictionary new_key=Fields[field]
#条件4ifTmp_val = ='NULL': Tmp_val=None
#条件2iffield = ='Rdf-schema#label': Tmp_val= Re.sub (r'\(.*\)',"', Tmp_val). Strip ()
#条件3iffield = ='name' andLine[field] = ='NULL': Tmp_val= line['Rdf-schema#label'].strip ()
#条件5iffield = ='synonym' andTmp_val:tmp_val=Parse_array (Line[field])
#子字典中所包含的的keyifNew_keyinch['Kingdom','Family','Order','Phylum','Genus','class']:
# The value of the key contained in the sub-dictionary is res['Classification'][new_key] =Tmp_valcontinue
#将新的key和val放入到res中, and then add it to the list to return Res[new_key]=tmp_val data.append (res)returnDatadefParse_array (v):
#解析数组
#如果以 {Start and end of}, delete the left and right {}, split with |, and finally remove the space for each item and returnif(V[0] = ="{") and(V[-1] = ="}"): v= V.lstrip ("{") v= V.rstrip ("}") V_array= V.split ("|") V_array= [I.strip () forIinchV_array]returnV_arrayreturn[V]
defTest ():
#测试函数, if there is no error, the result is correct data=process_file (datafile, fields)Print "Your First entry:"Pprint.pprint (data[0]) first_entry= { "synonym": None,"name":"Argiope", "Classification": { "Kingdom":"Animal", "Family":"orb-weaver Spider", "Order":"Spider", "Phylum":"arthropod", "Genus": None,"class":"arachnid" }, "URI":"Http://dbpedia.org/resource/Argiope_ (spider)", "label":"Argiope", "Description":"The genus Argiope includes rather large and spectacular spiders that often has a strikingly coloured abdomen. These spiders is distributed throughout the world. Most countries in tropical or temperate climates host one or more species that is similar in appearance. The etymology of the name is from a Greek name meaning silver-faced." } assertLen (data) = = 76assertData[0] = =First_entryassertdata[17]["name"] =="Ogdenia" assertdata[48]["label"] =="Hydrachnidiae" assertdata[14]["synonym"] == ["Cyrene Peckham & Peckham"]if __name__=="__main__": Test ()
Monogodb Practice A