From text file to dictionary

时间:2016-04-15 15:04:11

标签: python csv dictionary

I'm a txt file and taking the strings and making the first my key for my dictionary I'm creating and the rest will be my values as a tuple. There is header before hand and I've already made my code "ignore" it at the start.

Example of txt values:

"Ronald Reagan","1981","8","69","California","Republican"
"George Bush","1989","4","64","Texas","Republican"
"Bill Clinton","1993","8","46","Arkansas","Democrat"

I want to create dictionary that gives the following output:

{"Ronald Reagan": (1981,8,69,"California", "Republican") etc.}

This is what I currenltly have as my code :

def read_file(filename):
    d={}
    f= open(filename,"r")
    first_line = f.readline()
    for line in f:
        #line=line.strip('"')
        #line=line.rstrip()
        data=line.split('"')
        data=line.replace('"', "")

        print(data)


        key_data=data[0]

        values_data= data[1:]
        valuesindata=tuple(values_data)
        d[key_data]=valuesindata

    print(d)

read_file(filename)

The first print statement (I put it there just to see what the output at that point was and it gave me the following :

Ronald Reagan,1981,8,69,California,Republican
George Bush,1989,4,64,Texas,Republican

etc. By the time it gets to the second print statement it does the following:

{'R': ('o', 'n', 'a', 'l', 'd', ' ', 'R', 'e', 'a', 'g', 'a', 'n', ',', '1', '9', '8', '1', ',', '8', ',', '6', '9', ',', 'C', 'a', 'l', 'i', 'f', 'o', 'r', 'n', 'i', 'a', ',', 'R', 'e', 'p', 'u', 'b', 'l', 'i', 'c', 'a', 'n', '\n'), 'G': ('e', 'o', 'r', 'g', 'e', ' ', 'B', 'u', 's', 'h', ',', '1', '9', '8', '9', ',', '4', ',', '6', '4', ',', 'T', 'e', 'x', 'a', 's', ',', 'R', 'e', 'p', 'u', 'b', 'l', 'i', 'c', 'a', 'n', '\n')}

Also, I'm splitting it at the quotes because some of my strings contain a comma as part of the name, example : "Carl, Jr."

I'm not wanting to import the csv module, so is there a way to do that?

2 个答案:

答案 0 :(得分:3)

You can use the csv module like alecxe suggested or you can do it "manually" like so:

csv_dict = {}

with open(csv_file, 'r') as f:
    for line in f:
        line = line.strip().replace('"', '').split(',')
        csv_dict[line[0]] = tuple(int(x) if x.isdigit() else str(x) for x in line[1:])

This will remove the double quotes, cast numerical values to int and create a dictionary of tuples.

答案 1 :(得分:2)

The major problem in your code leading into this weird result is that data variable is a string, data[0] would give you the first character, data[1:] the rest - you needed to call split(",") to first split the string into the list.

I have a limitation to not import any modules.

The idea is to use split(",") to split each line into individual items and strip() to remove the quotes around the item values:

d = {}
with open(filename) as f:
    for line in f:
        items = [item.strip('"').strip() for item in line.split(",")]
        d[items[0]] = items[1:]

print(d)

Prints:

{'Bill Clinton': ['1993', '8', '46', 'Arkansas', 'Democrat'],
 'George Bush': ['1989', '4', '64', 'Texas', 'Republican'],
 'Ronald Reagan': ['1981', '8', '69', 'California', 'Republican']}

FYI, using csv module from the standard library would make things much easier:

import csv
from pprint import pprint

d = {}
with open(filename) as f:
    reader = csv.reader(f)
    for row in reader:
        d[row[0]] = row[1:]

pprint(d)

You can also use a dictionary comprehension:

d = {row[0]: row[1:] for row in reader}