Reputation: 2069
I have an excel file with 39 columns and 10000 rows.
I would like to store the key and value in list.
I can only come up with
result = []
temp = {}
for x in range (1, 10000):
for y in range (1, 39):
# title and sheet also come from the excel
temp[ title[y] ] : SHEET.cell_value(x, y)
result.append(temp)
The code I wrote didn't running as I expected so how can I rewrite it?
I would like to expect the result would be:
result = [
{'a':1, 'b':2, 'c':3, ...},
{'a':1, 'b':2, 'c':3, ...},
{'a':1, 'b':2, 'c':3, ...},
....
]
Upvotes: 0
Views: 54
Reputation: 1557
As others pointed out, using Pandas is probably the way to go. However, if you wish to implement a solution using plain Python, and assuming you have SHEET
and title
objects defined, this is probably what you want:
result = []
for x in range (1, 10000):
temp = {}
for y in range (1, 39):
# title and sheet also come from the excel
temp[ title[y] ] = SHEET.cell_value(x, y)
result.append(temp)
Upvotes: 1
Reputation: 15488
I will use Pandas. To install first run from Cmd or terminal:
pip install -U pandas
Then import excel sheet with read_excel
import pandas as pd
df = pd.read_excel("EXCEL SHEET PATH")
And then to dictionary to_dict:
print(df.to_dict())
Upvotes: 1
Reputation: 2514
Using pandas, it may be easier...
import pandas as pd
df = pd.read_excel(my_url)
print(df.to_dict('r'))
Upvotes: 1