Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 用Python将数组转换为字典_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 3.x 用Python将数组转换为字典

Python 3.x 用Python将数组转换为字典,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,我正在尝试刮取一个表并将其转换为字典,使用TH作为键,td作为值 下面是获取TD和TH的代码 for row in rows: td = row.find_all('td') th = row.find_all('th') row2 = [i.text.replace("\n", "").strip() for i in td] print(row2) ['', '90315', 'Printmaking I',

我正在尝试刮取一个表并将其转换为字典,使用TH作为键,td作为值

下面是获取TD和TH的代码

for row in rows:

        td = row.find_all('td')
        th = row.find_all('th')
        row2 = [i.text.replace("\n", "").strip() for i in td]
        print(row2)
        ['', '90315', 'Printmaking I', 'S1', '01(REG-HR)', 'Faletto, Liana', '445', 
        'LS']
        print(headers) 
        #['Class', 'Description', 'Term', 'Schedule', 'Primary Staff > Name', 'Clssrm', 'Name']
如何将输出转换为(删除第一个空白数组项)


删除第2行的第一个元素:(
>
是python提示符)

或者如果您的
标题
包含字典键列表:

for i in range(len(headers)):
   thisdict[headers[i]] = row2[i]
>>> del row2[0]
>>> row2
['90315', 'Printmaking I', 'S1', '01(REG-HR)', 'Faletto, Liana', '445', 'LS']
#!/usr/bin/python
thisdict = {'class':"", 'description':"", 'term':"", ...}
thisdict['class']=row2[1]
thisdict['description']=row2[2]
thisdict['term']=row2[3]
...

for i in range(len(headers)):
   thisdict[headers[i]] = row2[i]
>>> thisdict
{'Term': 'S1', 'Description': 'Printmaking I', 'term': 'S1', 'class': '90315', 'Class': '90315', 'description': 'Printmaking I'}