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, ...},
....
]
Using pandas, it may be easier...
import pandas as pd
df = pd.read_excel(my_url)
print(df.to_dict('r'))
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())
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)
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.