json - 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
解决方案
事实证明,问题在于某些地址是邮政信箱,一旦我将它们删除,代码就会按预期工作,
推荐阅读
- javascript - 在查询 API 端点之前无法获取地理位置数据
- bash - 如何使用 sed 命令替换文件中两个单引号之间的文本?
- machine-learning - 神经网络 :: 在不知道类总数的情况下进行分类
- reactjs - 如何将函数指定为具有默认值的钩子的可选参数
- python-3.x - Python + Selenium 需要新窗口
- sql - 如何运行将数据作为 nvarchar 传递的过程?
- java - 将二进制搜索转换为使用递归
- python - 在我第一次从 Docker PHPMyAdmin 连接到 mySQL 实例之前,无法从 Tortoise/Python 连接到 Docker mySQL 实例
- solana - 在命令行上通过 SPL 在 solana 上创建令牌时出现问题
- php - Laravel 自动将用户及其 pdf 文件存储在数据库中