create csv file from dict

Hello my question is how to save my dictionary in a csv file , I have managed to collect data from diffrent excel files and save it in a dictionary here's the code :

import os
from os import walk
import pandas as pd 

path = r'C:\Users\Sarah\Desktop\IOMTest'
my_files = []
for (dirpath, dirnames, filenames) in walk(path):
my_files.extend([os.path.join(dirpath, fname) for fname in filenames])


all_dicts_list = []
for file_name in my_files:

#Display sheets names using pandas
mosul_file = file_name
xl = pd.ExcelFile(mosul_file)
mosul_df = xl.parse(0, header=[1], index_col=[0,1,2])

#Read Excel and Select columns

mosul_file = pd.read_excel(file_name, sheet_name = 0 , 
index_clo=None, na_values= ['NA'], usecols = "A, E, G, H , L , M" )

#Remove NaN values

data_mosul_df = mosul_file.apply (pd.to_numeric, errors='coerce')
data_mosul_df = mosul_file.dropna()

#Save to Dictionary

datamosulx = data_mosul_df.to_dict()

My question is how to save

datamosulx = data_mosul_df.to_dict()

As a csv file ? Thank you

if somedict is your dictionary, you can use something like this:

with open('mycsvfile.csv','wb') as f:
    w = csv.writer(f)

How do I write a Python dictionary to a csv file?, You are using DictWriter.writerows() which expects a list of dicts, not a dict. You want DictWriter.writerow() to write a single row. You will also� I am trying to create a dictionary from a csv file. The first column of the csv file contains unique keys and the second column contains values. Each row of the csv file represents a unique key, value pair within the dictionary.

You can use to below two formats to convert pandas to dictionary.

Also It is one of the most widely used modules in python.

Type 1: Dictionary with list as values.

import pandas as pd

struct = {'ID' : ['1','2','3'], 'COST' : ['10','20','30']}

Type 2: List containing dictionaries with matching keys.

import pandas as pd

struct = [{'ID': '1', 'COST': '10'}, {'ID': '2', 'COST': '20'},{'ID': '3', 'COST': '30'}]

We use these structure in order to provide a structure to our data.

Learning References Link :

How to save a Python Dictionary to a CSV File?, csvwriter.writerows() method is used to write rows of data into the specified file. Note: To write a single dictionary in CSV file use writerow� The CSV module contains the DictWriter method that requires the name of the CSV file to write and a list object containing field names. The writeheader () method writes the first line in CSV file as field names. The subsequent for loop write each row in CSV form to the CSV file.

on dataframe object call to_csv('new_csv_filename.csv') function.

Try following code:

final_dataframe = pd.DataFrame(all_dicts_list)

How to write a dictionary to a .csv in Python, csv file file for writing. Call csv.writer(csvfile) with this stream as csvfile to create a writer object. Use a for-loop and dict� Takes in one variable called "variables_file" def csv_dict_list(variables_file): # Open variable-based csv, iterate over the rows and map values to a list of dictionaries containing key/value pairs reader = csv.DictReader(open(variables_file, 'rb')) dict_list = [] for line in reader: dict_list.append(line) return dict_list # Calls the csv_dict_list function, passing the named csv device_values = csv_dict_list(sys.argv[1]) # Prints the results nice and pretty like pprint.pprint

If you have a list of dict.

import csv
records = [{'first_name': 'Arun', 'age': 27, 'location': ['KL', 'IN']},
 {'last_name': 'N A', 'first_name': 'Samuel', 'age': 30}] #  Input. Here we have two rows and lets take the keys as column name.
heading_set = {k for row in records for k in list(row)} # Doing this since the keys are different in the given dict
print(f"heading is: {heading_set}")
with open('example1.csv', 'w') as opcsv:
    writer = csv.DictWriter(opcsv, fieldnames=list(heading_set))
    for row in records:
        for key in list(row):
            if type(row.get(key)) == list:
                row[key] = ",".join(row[key])

How to Export Dictionary Data as CSV in Python, The challenge in working with CSV files is that our data is often stored in a different format internally within Python memory. Python dictionaries� You are using DictWriter.writerows() which expects a list of dicts, not a dict. You want DictWriter.writerow() to write a single row. You will also want to use DictWriter.writeheader() if you want a header for you csv file. You also might want to check out the with statement for opening files. It's not only more pythonic and readable but handles closing for you, even when exceptions occur.

How To Write Python Dictionary To CSV File Example, CSV is the most common file format that is widely supported by many platforms and applications. Let's see how to convert Python dictionary to� To create a CSV file with a text editor, first choose your favorite text editor, such as Notepad or vim, and open a new file. Then enter the text data you want the file to contain, separating each value with a comma and each row with a new line. Title1,Title2,Title3 one,two,three example1,example2,example3 Save this file with the extension.csv.

csv — CSV File Reading and Writing — Python 3.8.5 documentation, Programmers can also read and write data in dictionary form using the DictReader and DictWriter classes. See also. PEP 305 - CSV File API. The Python� The csv module contains DictWriter method that requires name of csv file to write and a list object containing field names. The writeheader () method writes first line in csv file as field names. The subsequent for loop writes each row in csv form to the csv file.

write dictionary to csv python Code Example, Assign new ordered dict to store the order in which dictionary is inserted. 24. new_dict = OrderedDict(). 25. 26. #open a csv file with write mode� In this article, we will discuss how to append a row to an existing csv file using csv module’s reader / writer & DictReader / DictWriter classes. Suppose we have a CSV file students.csv, whose contents are, Id,Name,Course,City,Session 21,Mark,Python,London,Morning 22,John,Python,Tokyo,Evening 23,Sam,Python,Paris,Morning

  • the Dictionary with list as values is the one that i need how ever i am not sure ho to link it to my all_dicts_lists I have tried the following code but it seems to not work , i want to arrange my data by columns : datamosulx =[] struct = {'Place ID' : [], 'Families' : [], 'Latitude' : [], 'Longitude' : [], 'Location Name' : [], 'Individuals' : [] } pd.DataFrame(struct).to_csv(r'C:\Users\Sarah\Desktop\IOMTest\datamosulxx.csv',header=True,index=False) any hints ? thank you
  • Hi this code works well however i am trying to structure my data by column but it's not working datamosulx =[] struct = {'Place ID' : [], 'Families' : [], 'Latitude' : [], 'Longitude' : [], 'Location Name' : [], 'Individuals' : [] } pd.DataFrame(struct).to_csv(r'C:\Users\Sarah\Desktop\IOMTest\datamosulxx.csv',header=True,index=False)
  • Hi, I executed that code in jupyter, the csv file is also getting created and the columns are at the top horizontally left to right. with empty rows..
  • Yes how to make the empty rows filled with the data that are inside my dictionary datamosulx ?
  • if you want to pass a dictionary as a data to csv file then, one important point must be clear that is, when you pass dictionary into Dataframe() then, the keys will be treated as column and the values will be treated as rows, so what you can do is instead of creating columns through this pd.DataFrame(struct) add keys in your dictionary datamosulx as a structure columns (Place ID,Families etc) as a per your requirement. and finally pass that dictionary into DataFrame pd.DataFrame(datamosulx).to_csv('path\csv_filename.csv')