0

I am new to python and need help. I am trying to make a list of comma separated values. I have this data.

EasternMountain 84,844 39,754 24,509 286 16,571 3,409 315 
EasternHill 346,373 166,917 86,493 1,573 66,123 23,924 1,343 
EasternTerai 799,526 576,181 206,807 2,715 6,636 1,973 5,214 
CentralMountain 122,034 103,137 13,047 8 2,819 2,462 561 

Now how do I get something like this;

"EasternMountain": 84844,
"EasternHill":346373,

and so on??

So far I have been able to do this:

 fileHandle = open("testData", "r")
 data = fileHandle.readlines()
 fileHandle.close()

 dataDict = {}

 for i in data:
    temp = i.split(" ")

    dataDict[temp[0]]=temp[1]
    with_comma='"'+temp[0]+'"'+':'+temp[1]+','
    print with_comma
3
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. Commented Jul 10, 2013 at 11:28
  • For example, how do you have this data? In the form of a multiline string? As a file? Are those spaces between the values? If so, can there be other spaces that are not meant as value separators? Commented Jul 10, 2013 at 11:31
  • 1
    Sorry, I am also new to Stack Overflow.:) I have the data in a separate text file (Same as the way I have written in above) Commented Jul 10, 2013 at 11:35

2 Answers 2

3

Use the csv module

import csv
with open('k.csv', 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ')
    my_dict = {}
    for row in reader:
        my_dict[row[0]] = [''.join(e.split(',')) for e in row[1:]]

print my_dict

k.csv is a text file containing:

EasternMountain 84,844 39,754 24,509 286 16,571 3,409 315 
EasternHill 346,373 166,917 86,493 1,573 66,123 23,924 1,343 
EasternTerai 799,526 576,181 206,807 2,715 6,636 1,973 5,214 
CentralMountain 122,034 103,137 13,047 8 2,819 2,462 561 

Output:

{'EasternHill': ['346373', '166917', '86493', '1573', '66123', '23924', '1343', ''], 'EasternTerai': ['799526', '576181', '206807', '2715', '6636', '1973', '5214', ''], 'CentralMountain': ['122034', '103137', '13047', '8', '2819', '2462', '561', ''], 'EasternMountain': ['84844', '39754', '24509', '286', '16571', '3409', '315', '']}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. will try using this
If the solution answers your question, you might want to accept it. It will help other people with the same need. Thanks
I second that. I'm also a fairly new user to SO, and knowing which answer was accepted by the original poster (OP) has helped me figure out which answers to focus on.
So you should accept it as the good answer to your question. On the left hand-side of my post there is an accept button, just press it ;)
0

Try this:

def parser(file_path):
    d = {}
    with open(file_path) as f:
        for line in f:
            if not line:
                continue
            parts = line.split()
            d[parts[0]] = [part.replace(',', '') for part in parts[1:]]
    return d

Running it:

result = parser("testData")
for key, value in result.items():
    print key, ':', value

Result:

EasternHill : ['346373', '166917', '86493', '1573', '66123', '23924', '1343']
EasternTerai : ['799526', '576181', '206807', '2715', '6636', '1973', '5214']
CentralMountain : ['122034', '103137', '13047', '8', '2819', '2462', '561']
EasternMountain : ['84844', '39754', '24509', '286', '16571', '3409', '315']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.