简体   繁体   中英

How to import row from Excel file as list in Python?

I have a row of data in an Excel document file (say column P until column AD in row 370 in sheet1 ).

How do I import this specific row as a list in Python? I need it as a list to run a cubic spline interpolation and to get the interpolated values.

Do you have Pandas installed? The read_excel function is what you are looking for. This basically solves your problem.

import pandas
pandas.read_excel(
  'path_to_file.xlsx',
  usecols="P:AD",
  skiprows=369,
  nrows=1,
)

However you need to have the pandas package installed with the xlrd optional dependency. So if you have pip:

pip install pandas
pip install xlrd

Let me know if it works for you.

Try this.

import openpyxl 

# Give the location of the file 
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
wb_obj = openpyxl.load_workbook(path) 
sheet_obj = wb_obj.active
row_as_list = sheet_obj.rows[370] # required row as a list

first install pandas library by typing the following command in the command prompt

pip install pandas

later try this code

import pandas as pd
df = pd.read_excel('Book1.xlsx',sheet_name='Sheet1')
a = list(df.iloc[368][15:])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM