python讀取excel某列資料

2021-10-12 10:16:46 字數 2425 閱讀 5002

import xlrd

worksheet = xlrd.open_workbook(

'e:\\crawl\\000002.xls'

)sheet_names= worksheet.sheet_names(

)print

(sheet_names)

for sheet_name in sheet_names:

sheet = worksheet.sheet_by_name(sheet_name)

rows = sheet.nrows # 獲取行數

cols = sheet.ncols # 獲取列數,儘管沒用到

all_content =

cols = sheet.col_values(3)

# 獲取第二列內容, 資料格式為此資料的原有格式(原:字串,讀取:字串; 原:浮點數, 讀取:浮點數)

print

(cols)

print

(cols[3]

)print

(type

(cols[3]

))#檢視資料型別

輸出結果為:

[

'sheet1'][

'','',

'-72.20'

,'248.84'

,'-32.67'

,'156.93'

,'-49.58'

,'59.36',''

]248.84

<

class

'str'

>

import xlrd

worksheet = xlrd.open_workbook(

'e:\\crawl\\000002.xls'

)sheet_names= worksheet.sheet_names(

)print

(sheet_names)

for sheet_name in sheet_names:

sheet = worksheet.sheet_by_name(sheet_name)

rows = sheet.nrows # 獲取行數

cols = sheet.ncols # 獲取列數,儘管沒用到

all_content =

for i in

range

(rows)

: cell = sheet.cell_value(i,3)

# 取第二列資料

try:

cell =

float

(cell)

# 轉換為浮點數

except valueerror:

pass

print

(all_content)

print

(all_content[3]

)print

(type

(all_content[3]

))

結果為:

[

'sheet1'][

-72.2

,248.84,-

32.67

,156.93,-

33.53

,64.06,-

47.0

,117.33,-

31.6

,62.56,-

33.79

,30.63,-

59.65

,53.36,-

57.73

,34.15,-

60.0

,161.36,-

60.0

,41.3,-

50.77

,375.31,-

43.66

,167.79,-

57.47

,62.4,-

53.31

,98.65,-

37.41

,68.59,-

42.04

,54.28,-

58.9

,64.21,-

51.92

,36.0,-

49.02

,630.77,-

35.75

,160.82,-

51.98

,31.93,-

80.17

,198.75,-

35.77

,34.31,-

46.24

,84.36,-

46.82

,321.93,-

33.41

,120.85,-

49.58

,59.36

]156.93

<

class

'float'

>

Python 讀取excel指定的列

介紹了使用 xlrd 模組,讀取指定座標的單元格,以及迴圈整個 還沒有介紹如何讀取指定的列。目前有一張水果 表,內容如下 需要提取品名和成本價,完整 如下 usr bin env python3 coding utf 8 import xlrd 開啟excel檔案,建立乙個workbook物件,bo...

python 讀取 csv檔案某列遇到的問題

1.gbk codec can t decode byte 0xbd in position 3182 illegal multibyte sequence 換用 utf 8 編碼 2.utf 8 codec can t decode byte 0xae in position 3180 inval...

使用python讀取excel的列或者行

import os from openpyxl import load workbook wb load workbook r c users dell desktop 新建 microsoft excel 工作表.xlsx 檔名 sheet wb 1 表名 print 最大行 format she...