1

I am getting the following error for below piece of code..can anyone provide inputs on how to fix it?

from collections import OrderedDict
def main ():

    with open('gerrit_dependencylist.txt') as f:
        dic = OrderedDict()
        seen = set()
        for line in f:
            #print dic,line
            spl = line.split()
            #print "SPL"
            #print spl
            if len(spl) == 1:
                key = spl[0]
                v = ''
            else:
                print "LINE"
                print line
                key, v = spl[0], spl[1:]        
            for value in v:
                if value in dic and dic[value] == [""]:
                    del dic[value]
            for k1,v1 in dic.iteritems():
                if key in v1:
                    dic[k1].append(v)
                    break
            else:
                dic[key] = [v]

    with open('cherrypick_list.txt', 'w') as f:
        for k,v  in dic.items():
            f.write("{} {}\n".format(k," ".join(v)))

if __name__ == '__main__':
    main()

INPUT:-

KEY    VALUES
353311
344670 
332807 353314
338169 334478
334478 123456 34567
123456 98670
34567  11111  
353314 353311
348521 350166 350168 350169 350170 
350166 348521
350168 348521
350169 348521
350170 348521

CURRENT OUTPUT:-

344670 
332807 3 5 3 3 1 4 3 5 3 3 1 1
338169 3 3 4 4 7 8 1 2 3 4 5 6 3 4 5 6 7 9 8 6 7 0 1 1 1 1 1
348521 3 5 0 1 6 6 3 5 0 1 6 8 3 5 0 1 6 9 3 5 0 1 7 0 3 4 8 5 2 1 3 4 8 5 2 1 3 4 8 5 2 1 3 4 8 5 2 1
EXPECTED OUTPUT
344670
332807 353314 353311
338169 334478 123456 34567 98670 11111
348521 350166 350168 350169 350170

Error:-

Traceback (most recent call last):
  File "tesst.py", line 34, in <module>
    main()
  File "tesst.py", line 31, in main
    f.write("{} {}\n".format(k," ".join(v)))
TypeError: sequence item 0: expected string, list found
1
  • 4
    It means v is a list of lists; it should be a list of strings instead. Commented Jun 18, 2013 at 15:51

1 Answer 1

4

Except one case(344670 ['']) the value of v is always a list of lists, so you need to use a nested list comprehension to flatten the list first.

with open('cherrypick_list.txt', 'w') as f:
    for k,v  in dic.items():
        val = [item if isinstance(item, str) else " ".join(item) for item in v ]
        f.write("{} {}\n".format(k,val))

Get unique items:

val = [item if isinstance(item, str) else " ".join(item) for item in v ]
seen = set()
val = [x for x in val if x not in seen and not seen.add(x)]
Sign up to request clarification or add additional context in comments.

10 Comments

The problem is that the data is mixed. Sometimes v is a list of strings, sometimes it contains other lists with strings. You'll produce output with single characters separated with spaces in the other cases.
@MartijnPieters except one case(344670 ['']), it is always a list of lists.
For this sample it may be.
@AshwiniChaudhary - I incorporated your changes..output looks correct apart from that there is a space between every digit for the values.i updated the question with the current output..
@user2125827 I checked my code again and it seems to work fine on my system.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.