简体   繁体   中英

How to create a dictionary based off CSV file

I have a csv file with the following format:

#ID #Number #Date #Name #Email
1978 26   24/4/10 Jim  Jim@randomemail.com
1328 31   22/7/10 Jim  Kim@randomemail.com
1908 26   21/4/10 Jim  Dim@randomemail.com
1918 26   29/4/10 Jim  Rim@randomemail.com
1938 46   24/4/10 Jim  Lim@randomemail.com

I have opened the csv file and printed it out already.

I now want to make it so it's made into a dictionary such as: [ID: 1978, Number : 26, Date : 24/4/10, Name : Jim, Email : Jim@randomemail.com], [etc], [etc]

I know this is probably very easy but I'm new and have been stuck for a few hours.

Following up on my comment, consider something like:

import csv
with open('file.txt', 'r') as f:
    reader = csv.DictReader(f, delimiter=' ', skipinitialspace=True)
    for row in reader:
        print(row)

Output:

OrderedDict([('#ID', '1978'), ('#Number', '26'), ('#Date', '24/4/10'), ('#Name', 'Jim'), ('#Email', 'Jim@randomemail.com')])
OrderedDict([('#ID', '1328'), ('#Number', '31'), ('#Date', '22/7/10'), ('#Name', 'Jim'), ('#Email', 'Kim@randomemail.com')])
OrderedDict([('#ID', '1908'), ('#Number', '26'), ('#Date', '21/4/10'), ('#Name', 'Jim'), ('#Email', 'Dim@randomemail.com')])
OrderedDict([('#ID', '1918'), ('#Number', '26'), ('#Date', '29/4/10'), ('#Name', 'Jim'), ('#Email', 'Rim@randomemail.com')])
OrderedDict([('#ID', '1938'), ('#Number', '46'), ('#Date', '24/4/10'), ('#Name', 'Jim'), ('#Email', 'Lim@randomemail.com')])

The two extra arguments to DictReader are necessary to get your variable-space-delimited file to parse correctly.

Or, if you want all the rows at once, something like:

import csv
with open('file.txt', 'r') as f:
    reader = csv.DictReader(f, delimiter=' ', skipinitialspace=True)
    rows = list(reader)

print(rows)

produces

[
    OrderedDict([('#ID', '1978'), ('#Number', '26'), ('#Date', '24/4/10'), ('#Name', 'Jim'), ('#Email', 'Jim@randomemail.com')]), 
    OrderedDict([('#ID', '1328'), ('#Number', '31'), ('#Date', '22/7/10'), ('#Name', 'Jim'), ('#Email', 'Kim@randomemail.com')]), 
    OrderedDict([('#ID', '1908'), ('#Number', '26'), ('#Date', '21/4/10'), ('#Name', 'Jim'), ('#Email', 'Dim@randomemail.com')]), 
    OrderedDict([('#ID', '1918'), ('#Number', '26'), ('#Date', '29/4/10'), ('#Name', 'Jim'), ('#Email', 'Rim@randomemail.com')]), 
    OrderedDict([('#ID', '1938'), ('#Number', '46'), ('#Date', '24/4/10'), ('#Name', 'Jim'), ('#Email', 'Lim@randomemail.com')])
]

and,

print(rows[0]["#Email"])

produces

Jim@randomemail.com

Update

If your file is actually tab delimited, you could use:

reader = csv.DictReader(f, delimiter='\t')

You should be able to tell what the delimiter by printing the line (as you already have), but wrap it in a repr call -- something like print(repr(line)) . If you see a \\t in the output, it's tab delimited.

Here's some code written in pure python that'll do the trick:

for line in file_contents_2:
    line_contents = line.strip().split(",") # Removes the \n,
    # then turns the line into a list, where each value is seperated
    # by the comma      
    the_dictionary = {}
    reference = ["ORIN","DEST","HORIZ","BEAR"]
    for i in range(4): # iterates i=0 to i=3
        # Arrays start at 0, so a=[1,2,3]; a[1] would return 2
        the_dictionary[reference[i]] = line_contents[i]
    dictionary_list.append(the_dictionary)

Using pandas will make your life much easier:

import pandas as pd
df = pd.read_csv('path_to_your_csv')
your_dict = df.to_dict()

That's it, there are some optional arguments in to_dict to help you format it the way you want.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM