首页 > 解决方案 > JSONDecodeError:期望值:第 1 行第 1 列(字符 0),请求数据时!该代码工作了一段时间,然后它打破

问题描述

我正在尝试向人口普查局索取地理编码数据。到目前为止,代码运行并获得了超过 1450 条记录(我的总数约为 60K 记录),但随后它中断并返回此错误:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我的数据如下所示: 在此处输入图像描述

这是我的功能:

def get_fips(df):  
    
    num=len(ven_lst)
    for i,e in df.itertuples(index=False):
        if e not in repostory_lst:
            try:
                num+=1
                address=i
                vendor=e
                link="https://geocoding.geo.census.gov/geocoder/geographies/onelineaddress?address={0}&benchmark=Public_AR_Census2020&vintage=Census2020_Census2020&layers=10&format=json".format(address)
                reponse = requests.get(link).text
                reponse_1=json.loads(response)
                x=reponse_1['result']['addressMatches'][0]['geographies']['Census Blocks'][0]['GEOID'][:11]

                fields=[num,e,x]
                with open(r'fibs&ven.csv', 'a',newline='') as f: #because my data is big a save all the #data into this csv incase the code breaks
                    writer = csv.writer(f)
                    writer.writerow(fields)
            except (RuntimeError, TypeError, NameError,IndexError):
                pass
        elif e in repostory_lst:
            pass
    #df_result=pd.DataFrame(columns=['Vendor Code','fips'],index=range(len(fips_lst)))
    #df_result['Vendor Code']=ven_lst
    #df_result['fips']=fips_lst
    #x.to_csv('fibs&ven.csv', mode='a', header=False)
    return None

标签: jsonpython-3.xpython-requestsgeocodingjsondecoder

解决方案


事实证明,问题在于某些地址是邮政信箱,一旦我将它们删除,代码就会按预期工作,


推荐阅读