Hi all

I am newbie in python. I have a csv file with two columns first as source and second as target. There are multiple values assigned to keys in successive rows such as follows

1 -->4
1 -->5
1 -->8
1 -->12
2 -->4
2 -->17
2 -->14
2 -->46


I want to convert this into a format like dictionary or set as below
1 --> 4,5,8,12
2 --> 4,17,14,46


Can anyone help me please!


Thanks.

Do you have some code?

Can you post a sample csv file?

Happy codding!

use statement like:

if this_key in my_dict:
    my_dict[this_key].add(this_value)
else:
     my_dict[this_key] = {this_value}

I do not have any special code!

Please refer attached sample file.

Thanks!

import csv

d = {}

for row in csv.reader(open('sample.csv', "rb")):
    first, second = row[0].split('\t')
    if d.has_key(first.strip()):
        d[first.strip()] += [second.strip()]
    else:
        d[first.strip()] = [second.strip()] 

for k, v in d.iteritems():
    print k, v

Cheers, and Happy coding.

Any special reason to use csv module even you do strips and splits manually?

import csv

d = {}

for row in csv.reader(open('sample.csv', "rb")):
    first, second = row[0].split('\t')
    if d.has_key(first.strip()):
        d[first.strip()] += [second.strip()]
    else:
        d[first.strip()] = [second.strip()] 

for k, v in d.iteritems():
    print k, v

Cheers, and Happy coding.

Hi its giving following error

first, second = row[0].split('\t')
ValueError: need more than 1 value to unpack

Could you please advise me on this!

Thanks for reply!

That code worked for me, but here anyway my version of it:

d = {}

for row in open('sample.csv'):
    first, second = [ value.strip() for value in row.split('\t')]
    if first in d:
        d[first].append(second)
    else:
        d[first] = [second] 

for k, v in d.iteritems():
    print k, v
""" Output:
p3 ['e45', 'fgt9', 'ft678', 'lk89']
a2 ['e45', 'ghr55', 'er456', 'fgt9']
j67 ['st90', 'hk90h', 'er456', 'fgt9']
"""

Maybe header row problem?

import csv

d = {}

for row in csv.reader(open('sample.csv', "rb")):
    first, second = row[0].split('\t')
    if d.has_key(first.strip()):
        d[first.strip()] += [second.strip()]
    else:
        d[first.strip()] = [second.strip()] 

for k, v in d.iteritems():
    print k, v

Cheers, and Happy coding.

ValueError problem solved.. just replaced row[0].split('\t') by only row[0:]

Please find code below. Please suggest better option for writing to file, if any.

import csv
f2 = open('sample.txt', 'w')


d = {}

for row in csv.reader(open('sample.csv',"rb")):
first, second = row[0:]
if d.has_key(first.strip()):
d[first.strip()] += [second.strip()]
else:
d[first.strip()] = [second.strip()]

for k,v in d.iteritems():
print "%s\t%s\t\n" % (k, v)
f2.write("%s\t%s\t\n"%(k,v))
f2.close()


Cheers! thanks for the reply.

That code worked for me, but here anyway my version of it:

d = {}

for row in open('sample.csv'):
    first, second = [ value.strip() for value in row.split('\t')]
    if first in d:
        d[first].append(second)
    else:
        d[first] = [second] 

for k, v in d.iteritems():
    print k, v
""" Output:
p3 ['e45', 'fgt9', 'ft678', 'lk89']
a2 ['e45', 'ghr55', 'er456', 'fgt9']
j67 ['st90', 'hk90h', 'er456', 'fgt9']
"""

Maybe header row problem?

Hi tonyjv thanks for the code.. it is giving the same ValueError even after removing the header row.

Can you post a real sample CSV?

Can you post a real sample CSV?

Hi
Here is a real csv file.

Cheers!

I presume your using python 3, because the sample file works.

You can try like this bellow, or go for a aproach without the module as tonyvj said.

import csv

d = {}

for row in csv.reader(open('sample.csv')):
    first, second = [value.strip() for value in row[0].split('\t')]
    if d.has_key(first.strip()):
        d[first].append(second)
    else:
        d[first] = [second] 

f = open('output.txt', 'w')
for k, v in d.iteritems():
    print k, v
    f.write('%s %s\n' %(k, v))
f.close()

Cheers.

Happy coding!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.