次の形式でデータを操作しています。
name phone email website
Diane Grant Albrecht M.S.
Lannister G. Cersei M.A.T., CEP 111-222-3333 cersei@got.com www.got.com
Argle D. Bargle Ed.M.
Sam D. Man Ed.M. 000-000-1111 dman123@gmail.com www.daManWithThePlan.com
Sam D. Man Ed.M.
Sam D. Man Ed.M. 111-222-333 dman123@gmail.com www.daManWithThePlan.com
D G Bamf M.S.
Amy Tramy Lamy Ph.D.
そして、それを次のようにしたいと思います:
name phone email website area degree
Diane Grant Albrecht M.S.
Lannister G. Cersei 111-222-3333 cersei@got.com www.got.com CEP M.A.T.
Argle D. Bargle Ed.M.
Sam D. Man 000-000-1111, 111-222-3333 dman123@gmail.com dman123@gmail.com Ed.M.
D G Bamf M.S.
Amy Tramy Lamy Ph.D.
「名前」フィールドには、個人の名前、学位、および実践分野を含めることができます。
(また、2 番目の 2 つの「Sam D. Man...」エントリが欠落していることに気付くかもしれません。この質問では、それは無関係です。次の段階で、重複を削除します)
そこで、まずこの「名前」列を調べて、名前列を解析して、業務分野 (例: CEP) と学位 (例: Ph.D.) を分離しようとします。これらを作成したフィールド 'area' と 'degree' に書き込み、変更/短縮された名前を 'name' フィールドに保存しようとしました。このセクションの最後に、理想的には、各「名前」フィールドには個人の名前のみが含まれます。
ただし、スクリプトを実行しても、人の名前フィールドには影響しません。スクリプトを調整して名前を変更するにはどうすればよいですか?
ありがとう!
理解しやすくするためにコメントしたスクリプトは次のとおりです。
# Stores a list of dictionaries, each dictionary containing a person's entry with keys corresponding to variable names (ex: [{'name':'Sam', 'phone':'111-111-1111'...},{}])
myjson = []
# Add fields 'area' and 'degree' to store area of pract and deg earned, which will be parsed from the 'name' field
with(open("ieca_first_col_fake_text.txt", "rU")) as f:
sheet = csv.DictReader(f,delimiter="\t")
sheet.fieldnames.append('flag')
sheet.fieldnames.append('area')
sheet.fieldnames.append('degree')
for row in sheet:
myjson.append(row)
この時点で、「myjson」という辞書のリストができました。各ディクショナリは、データベースへのエントリを表します。「name」フィールドを見てみましょう。
degrees = ['M.A.T.','Ph.D.','MA','J.D.','Ed.M.', 'M.A.', 'M.B.A.', 'Ed.S.', 'M.Div.', 'M.Ed.', 'RN', 'B.S.Ed.', 'M.D.', 'M.S.']
# Parse name element
for row in myjson:
# check whether the name string has an area of practice by checking if there's a comma separator
if ',' in row['name']:
# separate area of practice from name and degree and bind this to var 'area'. If error, area is an empty list
split_area_nmdeg = row['name'].split(',')
try:
row['area'].append(split_area_nmdeg.pop())
except AttributeError:
row['area'] = []
# Split the name and deg by spaces. If there's a deg, it will match with one of elements and will be stored deg list. The deg is removed name_deg list and all that's left is the name.
split_name_deg = re.split('\s',split_area_nmdeg[0])
for word in split_name_deg:
for deg in degrees:
if deg == word:
try:
row['degree'].append(split_name_deg.pop())
except AttributeError:
row['degree'] = []
row['name'] = ' '.join(split_name_deg)
print row['name']
# if the name string does not contain a comma and therefore does not contain an area of practice
else:
row['area'] = []
split_name_deg = re.split('\s',row['name'])
for word in split_name_deg:
for deg in degrees:
try:
if deg == word:
row['degree'].append(split_name_deg.pop())
except AttributeError:
row['degree'] = []
row['name'] = ' '.join(split_name_deg)
print row['name']
出力を確認します。
for row in myjson:
print row
次のようになります。
{'website': '', 'name': 'Diane Grant Albrecht M.S.', 'degree': [], 'area': [], 'phone': '', 'flag': None, 'email': ''}
{'website': 'www.got.com', 'name': 'Lannister G. Cersei M.A.T.', 'degree': [], 'area': [], 'phone': '111-222-3333', 'flag': None, 'email': 'cersei@got.com'}
{'website': '', 'name': 'Argle D. Bargle Ed.M.', 'degree': [], 'area': [], 'phone': '', 'flag': None, 'email': ''}
{'website': 'www.daManWithThePlan.com', 'name': 'Sam D. Man Ed.M.', 'degree': [], 'area': [], 'phone': '000-000-1111', 'flag': None, 'email': 'dman123@gmail.com'}
{'website': '', 'name': 'Sam D. Man Ed.M.', 'degree': [], 'area': [], 'phone': '', 'flag': None, 'email': ''}
{'website': 'www.daManWithThePlan.com', 'name': 'Sam D. Man Ed.M.', 'degree': [], 'area': [], 'phone': '111-222-333', 'flag': None, 'email': ' dman123@gmail.com'}
{'website': '', 'name': 'D G Bamf M.S.', 'degree': [], 'area': [], 'phone': '', 'flag': None, 'email': ''}
{'website': '', 'name': 'Amy Tramy Lamy Ph.D.', 'degree': [], 'area': [], 'phone': '', 'flag': None, 'email': ''}
first_row {'website': '', 'name': 'Diane Grant Albrecht M.S.', 'degree': [], 'area': [], 'phone': '', 'email': ''}