': CSV file formation not working in excel

I'm trying to make my code use the keys from my 'data' directory, in this case 'name' and 'language', as headers in a csv file and the values as rows.

This is what the excel file should look like

This is how it currently looks, not formated in the way I want it to be

This is my current code:

import csv


data = {
    'name': ['Dave', 'Dennis', 'Peter', 'Jess'],
    'language': ['Python', 'C', 'Java', 'Python']
}

with open('mycvsfile.csv', 'w') as f:
    w = csv.DictWriter(f, data.keys())
    w.writeheader()
    w.writerow(data)

I've tried many different solutios but the formating is still off.



Solution 1:[1]

If you can use pandas:

import pandas as pd    
pd.DataFrame(data).to_csv('name.csv', index=False)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PaNh