《Python数据处理》9.1.6创建分组笔记:NameError:name text_type is not defined
一、现象
源码:
import json
from 数据集连接再测试 import cpi_and_cl
import pprint
import agate
path = 'I:\\360下载\\data-wrangling\\data\\chp9\\earth.json'
country_json = json.loads(open(path, 'r', encoding='utf-8').read())
country_dict = {}
for dct in country_json:
'''提取json形成单独对应的组'''
country_dict[dct['name']] = dct['parent']
#pprint.pprint(country_dict)
def get_country(country_row):
'''获得大洲的名称'''
return country_dict.get(country_row['Country / Territory'].lower())
#pprint.pprint(get_country())
#for r in cpi_and_cl().rows:
#print(r['Country / Territory'],r['continent'])
#continent:大洲
#print(cpi_and_cl().columns[0])
cpi_and_cl = cpi_and_cl().compute([('continent', agate.Formula(text_type, get_country)),])
报错:
NameError: name 'text_type' is not defined

二、解决方法
(一)查阅文档
链接: link.
但是文档中也是一样

(二)Github中的问题寻找
链接: link.



解决方法:
full_names = exonerations.compute([
('full_name', agate.Formula(agate.Text(), lambda row: '%(first_name)s %(last_name)s' % row))
])
将text_type改为agate.Text()
这样以后就可以了:


本文记录了在使用Python进行数据处理时遇到的NameError错误:'text_type'未定义的问题及解决方法。错误发生在agate库的Formula方法中,通过将'text_type'替换为'agate.Text()'解决了问题。

86

被折叠的 条评论
为什么被折叠?



