- 另外:有人可以告诉我为什么它没有选择“网站”吗?
我试图找出导致此错误的原因:
第 60 行:是什么导致了这个错误???仅出现在第一行:
UnboundLocalError: local variable 'name' referenced before assignment
代码:
import re
import json
import jsonpickle
from nameparser import HumanName
from pprint import pprint
import csv
import json
import jsonpickle
from nameparser import HumanName
from pprint import pprint
from string import punctuation, whitespace
def parse_ieca_gc(s):
########################## HANDLE NAME ELEMENT ###############################
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.']
degrees_list = []
# check whether the name string has an area / has a comma
if ',' in s['name']:
# separate area of practice from name and degree and bind this to var 'area'
split_area_nmdeg = s['name'].split(',')
area = split_area_nmdeg.pop()
print 'split area nmdeg'
print area
print split_area_nmdeg
# 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:
degrees_list.append(split_name_deg.pop())
name = ' '.join(split_name_deg)
# if the name string does not contain a comma, just parse as normal string
else:
area = []
split_name_deg = re.split('\s',s['name'])
for word in split_name_deg:
for deg in degrees:
if deg == word:
degrees_list.append(split_name_deg.pop())
name = ' '.join(split_name_deg)
# area of practice
category = area
# name
name = HumanName(name)
first_name = name.first
middle_name = name.middle
last_name = name.last
title = name.title
full_name = dict(first_name=first_name, middle_name=middle_name, last_name=last_name, title=title)
# degrees
degrees = degrees_list
# website
website = s.get('website','')
gc_ieca = dict(
name = name,
website = website,
degrees = degrees,
),
myjson = [] # myjson = list of dictionaries where each dictionary
with(open("ieca_first_col_fake_text.txt", "rU")) as f:
sheet = csv.DictReader(f,delimiter="\t")
for row in sheet:
myjson.append(row)
for i in range(4):
s = myjson[i]
a = parse_ieca_gc(s)
pprint(a)
示例数据(组成数据):
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
D G Bamf M.S.
Amy Tramy Lamy Ph.D.
Last login: Tue Jul 2 15:33:31 on ttys000
/var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup\ At\ Startup/ieca_first_col-394486416.142.py.command ; exit;
Samuel-Finegolds-MacBook-Pro:~ samuelfinegold$ /var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup\ At\ Startup/ieca_first_col-394486416.142.py.command ; exit;
range 4
split area nmdeg
CEP
['Lannister G. Cersei M.A.T.']
({'additionaltext': '',
'bio': '',
'category': ' CEP',
'certifications': [],
'company': '',
'counselingoptions': [],
'counselingtype': [],
'datasource': {'additionaltext': '',
'linktext': '',
'linkurl': '',
'logourl': ''},
'degrees': ['M.A.T.'],
'description': '',
'email': {'emailtype': [], 'value': 'cersei@got.com'},
'facebook': '',
'languages': 'english',
'linkedin': '',
'linktext': '',
'linkurl': '',
'location': {'address': '',
'city': '',
'country': 'united states',
'geo': {'lat': '', 'lng': ''},
'loc_name': '',
'locationtype': '',
'state': '',
'zip': ''},
'logourl': '',
'name': {'first_name': u'Lannister',
'last_name': u'Cersei',
'middle_name': u'G.',
'title': u''},
'phone': {'phonetype': [], 'value': '1112223333'},
'photo': '',
'price': {'costrange': [], 'costtype': []},
'twitter': '',
'website': ''},)
({'additionaltext': '',
'bio': '',
'category': [],
'certifications': [],
'company': '',
'counselingoptions': [],
'counselingtype': [],
'datasource': {'additionaltext': '',
'linktext': '',
'linkurl': '',
'logourl': ''},
'degrees': ['Ed.M.'],
'description': '',
'email': {'emailtype': [], 'value': ''},
'facebook': '',
'languages': 'english',
'linkedin': '',
'linktext': '',
'linkurl': '',
'location': {'address': '',
'city': '',
'country': 'united states',
'geo': {'lat': '', 'lng': ''},
'loc_name': '',
'locationtype': '',
'state': '',
'zip': ''},
'logourl': '',
'name': {'first_name': u'Argle',
'last_name': u'Bargle',
'middle_name': u'D.',
'title': u''},
'phone': {'phonetype': [], 'value': ''},
'photo': '',
'price': {'costrange': [], 'costtype': []},
'twitter': '',
'website': ''},)
({'additionaltext': '',
'bio': '',
'category': [],
'certifications': [],
'company': '',
'counselingoptions': [],
'counselingtype': [],
'datasource': {'additionaltext': '',
'linktext': '',
'linkurl': '',
'logourl': ''},
'degrees': ['Ed.M.'],
'description': '',
'email': {'emailtype': [], 'value': 'dman123@gmail.com'},
'facebook': '',
'languages': 'english',
'linkedin': '',
'linktext': '',
'linkurl': '',
'location': {'address': '',
'city': '',
'country': 'united states',
'geo': {'lat': '', 'lng': ''},
'loc_name': '',
'locationtype': '',
'state': '',
'zip': ''},
'logourl': '',
'name': {'first_name': u'Sam',
'last_name': u'Man',
'middle_name': u'D.',
'title': u''},
'phone': {'phonetype': [], 'value': '0000001111'},
'photo': '',
'price': {'costrange': [], 'costtype': []},
'twitter': '',
'website': ''},)
({'additionaltext': '',
'bio': '',
'category': [],
'certifications': [],
'company': '',
'counselingoptions': [],
'counselingtype': [],
'datasource': {'additionaltext': '',
'linktext': '',
'linkurl': '',
'logourl': ''},
'degrees': ['M.S.'],
'description': '',
'email': {'emailtype': [], 'value': ''},
'facebook': '',
'languages': 'english',
'linkedin': '',
'linktext': '',
'linkurl': '',
'location': {'address': '',
'city': '',
'country': 'united states',
'geo': {'lat': '', 'lng': ''},
'loc_name': '',
'locationtype': '',
'state': '',
'zip': ''},
'logourl': '',
'name': {'first_name': u'D',
'last_name': u'Bamf',
'middle_name': u'G',
'title': u''},
'phone': {'phonetype': [], 'value': ''},
'photo': '',
'price': {'costrange': [], 'costtype': []},
'twitter': '',
'website': ''},)
logout
[Process completed]