有很多方法可以给猫剥皮,但如果您只是阅读 .csv(而不是 .xlsx),则不需要 Excel 特定的库:
# this code just (over)writes a .csv for demonstration purposes
from csv import writer
with open('block.csv', 'w', newline='') as f:
data = [[(r+1)*100 + c+1 for c in range(15)] for r in range(9)]
cw = writer(f)
cw.writerows(data)
# this is how you can do it with pandas
import pandas as pd
df = pd.read_csv('block.csv', header=None)
# prints the entire dataframe
print(df)
# this selects the data you want (A2:A7), but it's still a dataframe
print(df.loc[1:6, 0:0])
# this selects the data, and the first columns from it, and then turns the values into a list
print(df.loc[1:6, 0:0][0].values.tolist())
# similarly, this selects B1:K1, turns the values into a list of lists and then selects the first
print(df.loc[0:0, 1:10].values.tolist()[0])
# you can also do it with the standard csv module
from csv import reader
# opening the file for reading
with open('block.csv', 'r') as f:
cr = reader(f)
# the reader will read the rows as a list of strings, we want numbers
data = [[int(x) for x in row] for row in list(cr)]
# prints the entire list of lists
print(data)
# this selects the first element from rows 1 through 6, so A2:A7
print([x[0] for x in data[1:7]])
# and from the first row, columns 2 through 11, so B1:K1
print(data[0][1:11])
# you don't need the csv module, although I would not recommend this, unless you must and you're
# certain your .csv doesn't have any problems
with open('block.csv', 'r') as f:
# just reading and splitting the lines and converting the values to integer
data = [[int(x) for x in line.strip().split(',')] for line in f]
# the rest of the code is the same as for the csv.reader
print(data)
print([x[0] for x in data[1:7]])
print(data[0][1:11])
作为奖励,这里有一个函数,允许您只使用 Excel 中的范围,以避免计数错误(就像我最初发布答案时所做的那样):
import re
def col_to_index(col):
return sum((ord(c) - 64) * 26**i for i, c in enumerate(reversed(col))) - 1
def df_excel_range(df, excel_range):
import re
coords = [(col_to_index(cell[0]), int(cell[1])-1) for cell in [
re.match('([A-Z]+)(\d+)', cell).groups() for cell in excel_range.upper().split(':')]]
if len(coords) == 1:
coords = [*coords[0], *coords[0]]
elif len(coords) != 2:
raise SyntaxError(f'not a valid range {excel_range}')
return df.loc[coords[0][1]:coords[1][1], coords[0][0]:coords[1][0]]
您可以使用它从数据框中选择一个范围,如下所示:
print(df_excel_range(df, 'A2:A7')[0].values.tolist())