Reputation: 584
I'm working on small project using Python i try to refactor my pd.read_excel() result but i get only the last row in my data variable:
this is my code :
@action(methods=['post'], detail=False)
def bulkImport(self, request, pk=None):
file_uploaded = request.FILES.get('file')
context = pd.read_excel(file_uploaded, engine='openpyxl')
data = {}
for column in context:
for b in context[column].tolist():
data[column] = b
print(data)
Native read_excel() result :
{
"name": [
"amine",
"bill"
],
"company": [
"bouhaddi",
"microsoft"
],
"email": [
"[email protected]",
"[email protected]"
],
"phone": [
43822510594,
559485556555
],
"adress": [
"3820 albert st",
"new york"
]
}
Expected Result :
{
'name' : 'value',
'phone' : 'value',
'email' : 'value',
'company' : 'value',
'adress' : 'value',
},
{
'name' : 'value',
'phone' : 'value',
'email' : 'value',
'company' : 'value',
'adress' : 'value',
}
Upvotes: 0
Views: 33
Reputation: 5347
context={
"name": [
"amine",
"bill"
],
"company": [
"bouhaddi",
"microsoft"
],
"email": [
"[email protected]",
"[email protected]"
],
"phone": [
43822510594,
559485556555
],
"adress": [
"3820 albert st",
"new york"
]
}
data=[{k:v[i] for k,v in context.items() } for i in range(2) ]
Output:
[{'name': 'amine',
'company': 'bouhaddi',
'email': '[email protected]',
'phone': 43822510594,
'adress': '3820 albert st'},
{'name': 'bill',
'company': 'microsoft',
'email': '[email protected]',
'phone': 559485556555,
'adress': 'new york'}]
Using list comprehension
and dictionary comprehension
can give you the two dicts within a list
Upvotes: 1