IndexError: 列表索引超出范围,位置在 table_data[headers[len(table_data)]] = values

huangapple go评论109阅读模式
英文:

IndexError: list index out of range at table_data[headers[len(table_data)]] = values

问题

  1. 我有这个Python脚本它读取一个HTML文件然后从中创建一个数据库表但是我在第`table_data[headers[len(table_data)]] = values`行遇到了`IndexError: list index out of range`错误我在Python方面的经验不多但我尝试过在for循环和`len(table_data)`中尝试一些东西
  2. bs4模块导入BeautifulSoup
  3. # 指定您的HTML文件的路径
  4. html_file_path = 'C:/Users/tom/service/soc/Soc.html'
  5. # 读取HTML文件的内容
  6. with open(html_file_path, 'r') as file:
  7. html = file.read()
  8. # 解析HTML
  9. soup = BeautifulSoup(html, 'html.parser')
  10. # 在HTML中查找所有表格
  11. tables = soup.find_all('table')
  12. # 遍历表格
  13. for table in tables:
  14. # 找到表格的ID属性
  15. table_id = table.get('id')
  16. # 提取表头
  17. headers = [th.get_text() for th in table.find('thead').find_all('th')]
  18. # 创建一个字典来存储表格数据
  19. table_data = {}
  20. # 遍历表格行
  21. for row in table.find('tbody').find_all('tr'):
  22. # 提取行单元格
  23. cells = row.find_all('td')
  24. # 提取单元格的值
  25. values = [cell.get_text().strip() for cell in cells]
  26. # 将值与其对应的表头存储在字典中
  27. table_data[headers[len(table_data)]] = values
  28. # 生成PostgreSQL表格脚本
  29. create_table_script = f"CREATE TABLE {table_id} (/n"
  30. for header, values in table_data.items():
  31. create_table_script += f" {header} {', '.join(values)},/n"
  32. create_table_script = create_table_script.rstrip(',/n') + "/n);/n"
  33. # 打印表格脚本
  34. print(create_table_script)
英文:

I have this python script that reads a html file then creates a database table from that but I'm getting a IndexError: list index out of range error on line table_data[headers[len(table_data)]] = values I haven't worked much with python a lot but I've tried stuff with the for loop and the len(table_data)

  1. from bs4 import BeautifulSoup
  2. # Specify the path to your HTML file
  3. html_file_path = 'C:/Users/tom/service/soc/Soc.html'
  4. # Read the contents of the HTML file
  5. with open(html_file_path, 'r') as file:
  6. html = file.read()
  7. # Parse the HTML
  8. soup = BeautifulSoup(html, 'html.parser')
  9. # Find all the tables in the HTML
  10. tables = soup.find_all('table')
  11. # Iterate over the tables
  12. for table in tables:
  13. # Find the table's ID attribute
  14. table_id = table.get('id')
  15. # Extract the table headers
  16. headers = [th.get_text() for th in table.find('thead').find_all('th')]
  17. # Create a dictionary to store the table data
  18. table_data = {}
  19. # Iterate over the table rows
  20. for row in table.find('tbody').find_all('tr'):
  21. # Extract the row cells
  22. cells = row.find_all('td')
  23. # Extract the cell values
  24. values = [cell.get_text().strip() for cell in cells]
  25. # Store the values with their corresponding headers in the dictionary
  26. table_data[headers[len(table_data)]] = values
  27. # Generate the PostgreSQL table script
  28. create_table_script = f"CREATE TABLE {table_id} (/n"
  29. for header, values in table_data.items():
  30. create_table_script += f" {header} {', '.join(values)},/n"
  31. create_table_script = create_table_script.rstrip(',/n') + "/n);/n"
  32. # Print the table script
  33. print(create_table_script)

答案1

得分: 1

你没有提供完整的错误消息,但我认为我看到了问题。我将假设table_dataheaders是两个相关列表,长度相同。请记住,列表索引从零开始,因此len(lst)返回的是最后一个索引之上的数字。

举个例子:

  1. lst = [1, 2, 3]
  2. print(lst[len(lst)]) # IndexError: list index out of range

假设headerstable_data的长度相同,那么len(table_data)返回的数字超出了这两个列表的范围。如果你想获取列表中的最后一项,只需使用lst[-1]

英文:

You didn't provide a full error message, but I think I see the problem. I'll make the assumption that table_data and headers are two related list of the same length. Remember that list indexes start with zero, therefore len(lst) returns the number above the number of the last index.

To illustrate:

  1. lst = [1, 2, 3]
  2. print(lst[len(lst)]) # IndexError: list index out of range

Assuming headers and table_data are of the same length, len(table_data) returns a number out of the range of both lists. If you were trying to get the last item in the list, just use lst[-1].

huangapple
  • 本文由 发表于 2023年7月13日 22:49:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定