为什么我会收到一条错误消息,指出要在Python中强制转换为unicode?

时间:2019-07-16 23:48:24

标签: python error-handling opencsv

我正在尝试编写代码,以从csv中获取api作为输入:

from pyzillow.pyzillow import ZillowWrapper, GetDeepSearchResults, GetUpdatedPropertyDetails

def get_zillowinfo(address,zipcode):
    zillow_data = ZillowWrapper('X1-ZWz17seirkzuh7_93aho')
    deep_search_response = zillow_data.get_deep_search_results(address,zipcode)
    result1 = GetDeepSearchResults(deep_search_response) #get zillowid from address
    updated_property_details_response = zillow_data.get_updated_property_details(result1.zillow_id)
    result2 = GetUpdatedPropertyDetails(updated_property_details_response) # get detail property info
    result = result2.home_info
    return result

print get_zillowinfo('73 main blvd','01545')

#2
import csv
with open(r'C:\Users\bca\Desktop\Pricing Study-Zillow\sample4.csv', 'r') as csvfile:
    spamreader = csv.reader(csvfile,delimiter=',')
    next(spamreader)
    for row in spamreader:
        print row


#3
import csv
with open(r'C:\Users\bca\Desktop\Pricing Study-Zillow\sample4.csv', 'r') as csvfile:
    spamreader = csv.reader(csvfile,delimiter=',')
    next(spamreader)
    for row in spamreader:
            print get_zillowinfo(row[0],row[1])

执行步骤3时,出现错误:

Traceback (most recent call last):

  File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2895, in run_code
    self.showtraceback()

  File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 1828, in showtraceback
    self._showtraceback(etype, value, stb)

  File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\ipykernel\zmqshell.py", line 547, in _showtraceback
    u'evalue' : py3compat.safe_unicode(evalue),

  File "C:\Users\bca\AppData\Local\Continuum\anaconda2\lib\site-packages\ipython_genutils\py3compat.py", line 65, in safe_unicode
    return unicode_type(e)

TypeError: coercing to Unicode: need string or buffer, dict found

为什么会这样?是因为我的字符不是字符串吗?在这种情况下,如何将所有数据更改为字符串?

这是我的数据集的repex: enter image description here

我需要在代码中进行哪些更改以避免该类型错误?

0 个答案:

没有答案