1. 準(zhǔn)備數(shù)據(jù)
在此習(xí)題集中痛垛,你將處理另一種類(lèi)型的 infobox 數(shù)據(jù)草慧,審核、清理數(shù)據(jù)匙头,并得出一種數(shù)據(jù)模型漫谷,將數(shù)據(jù)插入 MongoDB,然后對(duì)數(shù)據(jù)庫(kù)運(yùn)行一些查詢(xún)蹂析。數(shù)據(jù)集中包含關(guān)于蛛形綱動(dòng)物的數(shù)據(jù)舔示。
對(duì)于這道練習(xí),你的任務(wù)是解析文件电抚,僅處理 FIELDS 字典中作為鍵的字段惕稻,并返回清理后的值字典列表。
你應(yīng)該完成以下幾個(gè)步驟:
- 根據(jù) FIELDS 字典中的映射更改字典的鍵
- 刪掉“rdf-schema#label”中的小括號(hào)里的多余說(shuō)明蝙叛,例如“(spider)”
- 如果“name”為“NULL”俺祠,或包含非字母數(shù)字字符,將其設(shè)為和“l(fā)abel”相同的值借帘。
- 如果字段的值為“NULL”蜘渣,將其轉(zhuǎn)換為“None”
- 如果“synonym”中存在值,應(yīng)將其轉(zhuǎn)換為數(shù)組(列表)肺然,方法是刪掉“{}”字符蔫缸,并根* 據(jù)“|” 拆分字符串。剩下的清理方式將由你自行決定际起,例如刪除前綴“*”等拾碌。如果存在單數(shù)同義詞,值應(yīng)該依然是列表格式加叁。
- 刪掉所有字段前后的空格(如果有的話)
輸出結(jié)構(gòu)應(yīng)該如下所示:
[ { '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", "Two"],
'classification': {
'family': 'Orb-weaver spider',
'class': 'Arachnid',
'phylum': 'Arthropod',
'order': 'Spider',
'kingdom': 'Animal',
'genus': None
}
},
{ 'label': ... , }, ...
]
import codecs
import csv
import json
import pprint
import re
DATAFILE = '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'}
def process_file(filename, fields):
process_fields = fields.keys()
data = []
with open(filename, "r") as f:
reader = csv.DictReader(f)
for i in range(3):
l = reader.next()
for line in reader:
# YOUR CODE HERE
set = {}
set['classification'] = {}
for i in process_fields:
new_field = fields[i]
temp_value = line[i].strip()
if temp_value == 'NULL':
temp_value = None
if i=='rdf-schema#label':
temp_value = re.sub(r'\(.*\)',' ',temp_value).strip()
if i=='name' and line[i] =='NULL':
temp_value = line['rdf-schema#label'].strip()
if i=='synonym' and temp_value:
temp_value = parse_array(line[i])
if new_field in ["kingdom", "family", "order", "phylum", "genus", "class"]:
set['classification'][new_field] = temp_value
continue
set[new_field] = temp_value
data.append(set)
pass
return data
def parse_array(v):
if (v[0] == "{") and (v[-1] == "}"):
v = v.lstrip("{")
v = v.rstrip("}")
v_array = v.split("|")
v_array = [i.strip() for i in v_array]
return v_array
return [v]
2. 向 MongoDB 插入數(shù)據(jù)
Complete the insert_data function to insert the data into MongoDB.
import json
def insert_data(data, db):
db.arachnid.insert(data)
# Your code here. Insert the data into a collection 'arachnid'
pass
if __name__ == "__main__":
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017")
db = client.examples
with open('arachnid.json') as f:
data = json.loads(f.read())
insert_data(data, db)
print db.arachnid.find_one()