第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何遍歷字典列表并插入sqlite 3

如何遍歷字典列表并插入sqlite 3

一只甜甜圈 2023-03-16 16:28:17
我是編碼的初學(xué)者,如果我犯了任何錯(cuò)誤,我深表歉意。我在網(wǎng)上抓取了一個(gè)網(wǎng)站并從中得到了一個(gè)表格列表。我正在嘗試使用 for 循環(huán)將該數(shù)據(jù)插入到 sqlite 3 中,但返回時(shí)出現(xiàn)錯(cuò)誤。import requestsfrom bs4 import BeautifulSoupimport json##from  tkinter import *##bobert=Tk()##bobert.geometry("600x600")import sqlite3##cursor.execute("CREATE TABLE IF NOT EXISTS covid (name STRING, passward STRING, score INTEGER)")connection = sqlite3.connect('covidproject.db')cursor = connection.cursor()##cursor.execute("DROP TABLE IF EXISTS covid ")##cursor.execute("CREATE TABLE IF NOT EXISTS covid (name STRING, confirmed REAL, changes_today REAL,deceased REAL,active REAL, recovered REAL)")url = 'https://ncov2019.live/'headers = {'User-Agent':'Mozilla/5.0'}response = requests.get(url, headers = headers)response.status_codesoup = BeautifulSoup(response.content,'html.parser')stat_table = soup.find_all("table", attrs={"class": "display responsive"})headers = [header.get_text(strip=True) for header in soup.find_all("th")]rows = [dict(zip(headers, [td.get_text(strip=True) for td in row.find_all("td")]))        for row in soup.find_all("tr")[1:-1]]for i,x in enumerate(rows,9):    cursor.execute("INSERT INTO covid VALUES('"+rows[i]['Name']+"','"+rows[i]['Confirmed']+"','"+rows[i]['Changes Today']+"','"+rows[i]['Deceased']+"','"+rows[i]['Active']+"','"+rows[i]['Recovered']+"')")    connection.commit()##print (json.dumps(rows[9], indent=2))##row2=rows[9]##print (rows[9]['Name'])##print (rows[9])這是錯(cuò)誤:Traceback (most recent call last):  File "C:\Users\minio\Downloads\sqlite-tools-win32-x86-3310100\sqlite-tools-win32-x86-3310100\webscraping3.py", line 31, in <module>    cursor.execute("INSERT INTO covid VALUES('"+rows[i]['Name']+"','"+rows[i]['Confirmed']+"','"+rows[i]['Changes Today']+"','"+rows[i]['Deceased']+"','"+rows[i]['Active']+"','"+rows[i]['Recovered']+"')")KeyError: 'Name'
查看完整描述

1 回答

?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊

錯(cuò)誤消息KeyError: 'Name'表明您的詞典沒(méi)有“名稱”鍵。你可以做幾件事來(lái)診斷它;

首先要做的最簡(jiǎn)單的事情就是檢查基本情況,它是否有名稱條目。IE

print( rows[0]['Name'] )

如果可行,您可能想要做的下一件事是獲得有關(guān)哪一行失敗的更好信息,這樣的事情可能可行:

for i,x in enumerate(rows,9):
    print( rows[i]['Name'] )
    cursor.execute("INSERT INTO covid VALUES('"+rows[i]['Name']+"','"+rows[i]['Confirmed']+"','"+rows[i]['Changes Today']+"','"+rows[i]['Deceased']+"','"+rows[i]['Active']+"','"+rows[i]['Recovered']+"')")
    connection.commit()

您還可以使用像https://realpython.com/python-debugging-pdb/這樣的教程,使用 Python 調(diào)試器單步執(zhí)行代碼?;蛘撸梢钥紤]使用 try/except 來(lái)忽略特定錯(cuò)誤。


查看完整回答
反對(duì) 回復(fù) 2023-03-16
  • 1 回答
  • 0 關(guān)注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)