how to write a dictionary with one key multiple values to a csv file

后端 未结 3 1335
南笙
南笙 2021-01-27 07:13

I have a dictionary of the following form

>>> {\'1\' : [V3210 , 234567 ,1235675 , 23], \'2\' : [v3214 , 5678 ,65879 ,89} , ...}

how to

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-27 08:07

    Assuming the numeric keys are line numbers, try something like this:

    theDict = {'1' : [V3210 , 234567 ,1235675 , 23], '2' : [v3214 , 5678 ,65879 ,89] }
    largestkey=max(map(int,theDict.keys()))
    with file("out.csv") as f:
        for linenum in range(largestkey+1):
            f.write(",".join(theDict[str(linenum)])
            f.write("\n")
    

    You'll want to look at Python's string API, list handling, and file handling to get more familiar with Python...

提交回复
热议问题