AttributeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_8532/2479922506.py in <module> 1 import xlrd 2 import numpy as np ----> 3 wb=xlrd.open("历年总人口.xls") 4 sheet=wb.sheet_by_index(0) 5 col_0=sheet.col_values(0) AttributeError: module 'xlrd' has no attribute 'open'
时间: 2024-03-03 10:49:18 浏览: 124
这个错误提示是因为 `xlrd` 模块中没有 `open` 这个函数,正确的函数是 `open_workbook`。所以需要将 `xlrd.open` 改成 `xlrd.open_workbook`。
下面是修改后的代码:
```python
import xlrd
import numpy as np
wb = xlrd.open_workbook("历年总人口.xls")
sheet = wb.sheet_by_index(0)
col_0 = sheet.col_values(0)[38:]
col_1 = sheet.col_values(1)[38:]
col_2 = sheet.col_values(2)[38:]
col_4 = sheet.col_values(4)[38:]
year = [int(c) for c in col_0]
total = [int(c) for c in col_1]
man = [int(c) for c in col_2]
woman = [int(c) for c in col_4]
m = len(year)
arr = np.array(year).reshape(m,1)
arr = np.insert(arr,1,values=total,axis=1)
arr = np.insert(arr,1,values=man,axis=1)
arr = np.insert(arr,1,values=woman,axis=1)
file='历年总人口.csv'
np.savetxt(file,arr,fmt='%i',delimiter=',',comments='',header='年份,年末总人口,男性人口,女性人口')
x = np.loadtxt(file,dtype=np.int,delimiter=',',skiprows=1)
print(x)
```
阅读全文