Reputation: 9763
I am trying to pull specific values from a dictionary but my program keeps throwing a TypeError on some values from the dictionary. Can someone tell me why?
python program
import csv,os,re,sys
input_dict = csv.DictReader(open("./MCPlayerData/AllPlayerData2.csv"))
def list_scale_values(regexmatch,stopOnNA,person):
data=[]
for key in person:
if re.match(regexmatch, key):
try:
data.append(int(person[key]))
except (ValueError):
data.append('NA')
if(stopOnNA):
data=['NA'] #if any NA return NA
break
return data
try:
for person in input_dict:
print(list_scale_values(r'npi[0-9]+',True,person))
except (TypeError):
type, value, traceback = sys.exc_info()
print(type)
print(value)
print(traceback)
print '\n---\n',person,'\n---'
sys.exit()
print('DONE')
error output
Traceback (most recent call last):
File "correlations.py", line 22, in <module>
print(list_scale_values(r'npi[0-9]+',True,person))
File "correlations.py", line 9, in list_scale_values
if re.match(regexmatch, key):
File "/usr/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or buffer
Upvotes: 1
Views: 141
Reputation: 2020
To avoid this problem check if the key is a valid string or buffer before a regular expression is made.
so instead of if re.match(regexmatch, key):
write the following code
if key != None and re.match(regexmatch, key):
To answer the question why it would happen for some person
in the csv dictionary?
If the csv file does not take into consideration for all the columns in a row for each row this situation is totally possible for only certain rows, in this case people
To give an example :
consider the following CSV file
name,type,address,value
zoo,1,111,2
foo,2,222,,
bar,3,333,5
This would get the following results for the persons in csv.DictReader(open("filename.csv"))
{'type': '1', 'name': 'abc', 'value': '2', 'address': '111'}
{None: [''], 'type': '2', 'name': 'foo', 'value': '', 'address': '222'}
{'type': '3', 'name': 'bar', 'value': '5', 'address': '333'}
So in this case it would work for people[0]
and people[2]
but fail at people[1]
Upvotes: 1