Answers for "python csv to dictionary"

5

python csv dict reader

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Posted by: Guest on August-13-2020
2

python dictionary to csv

import csv
csv_columns = ['word','matchin']

csv_file = "found.csv"
try:
    with open(csv_file, 'w') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
        writer.writeheader()
        for data in corpus:
            writer.writerow(data)
except IOError:
    print("I/O error")
Posted by: Guest on February-17-2021
1

read csv and store in dictionary python

import csv

with open('coors.csv', mode='r') as infile:
    reader = csv.reader(infile)
    with open('coors_new.csv', mode='w') as outfile:
        writer = csv.writer(outfile)
        mydict = {rows[0]:rows[1] for rows in reader}
Posted by: Guest on August-28-2020
0

how to read a csv file and create a dictionary in c#

private Dictionary<string, string> ReadCsvFile(string pathToCsvFile)
  {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            using (var reader = new StreamReader(pathToCsvFile))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (line == null) continue;
                    var values = line.Split(';');
                    dictionary.Add(values[0],values[1]);
                }
            }

            return dictionary;
 }

// or
var dict = File.ReadLines(pathToCsvFile).Select(line => line.Split(';')).ToDictionary(line => line[0], line => line[1]);
Posted by: Guest on October-09-2020
0

csv library python convert dict to csv

import csv
my_dict = {'1': 'aaa', '2': 'bbb', '3': 'ccc'}
with open('test.csv', 'w') as f:
    for key in my_dict.keys():
        f.write("%s,%sn"%(key,my_dict[key]))
Posted by: Guest on August-24-2021
1

python create dictionary from csv

mydict = {y[0]: y[1] for y in [x.split(",") for x in open('file.csv').read().split('n') if x]}
Posted by: Guest on October-02-2020

Code answers related to "python csv to dictionary"

Python Answers by Framework

Browse Popular Code Answers by Language