So far, I have been trying to use "openpyxl" to accomplish this task. What I am trying to do is, pull all cell values from rows in an excel sheet and then include those cell values as part of a list of tuples. Here is my code so far:
from openpyxl import load_workbook
workbook = load_workbook('work_1.xlsx')
worksheet = workbook.get_sheet_by_name('Sheet1')
sheet_cells = []
for rows in worksheet.iter_rows():
for cell in rows:
sheet_cells.append(cell.value)
print sheet_cells
My results: [1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L]
By doing this, I am including all cell values (regardless of the row they are pulled from) into 1 single list.
What I want to accomplish is, have a list with values from each row included as tuples, like this: [(1L,2L,3L,4L),(5L,6L,7L,8L),(9L,10L,11L,12L)]
In the example above, my worksheet is using 3 rows.
I am a bit stuck (and new to working with Excel data in Python) and couldn't find any similar answers to this type of question. Thanks.
try:
from openpyxl import load_workbook
workbook = load_workbook('work_1.xlsx')
worksheet = workbook.get_sheet_by_name('Sheet1')
sheet_cells = []
for rows in worksheet.iter_rows():
row_cells = []
for cell in rows:
row_cells.append(cell.value)
sheet_cells.append(tuple(row_cells))
print sheet_cells
Have you tried Pandas for your work?
import pandas as pd
file=pd.read_csv(filename,header=None)
new_list=[]
for i in range(file.shape[1]):
new_list.append(list(file[i]))
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.