0

I have a dictionary in a txt format - this is part of the file (it contains 6000 keys:values:

{'YAL008W': 25, 'YBR255W': 50, 'YGR164W': 37, 'YGR131W': 40, 'YNL003C': 11, 'YBR135W': 2, 'YBR160W': 6, 'YJL082W': 79, 'YJL142C': 4, 'YPL191C': 38, 'YGL215W': 31, 'YKL074C': 33, 'YJL077C': 67, 'YKL096W-A': 22, 'YIL124W': 60, 'YLR364C-A': 2, 'YPL039W': 58, 'YNL170W': 16, 'YGL141W': 62, 'YJL179W': 15, 'YDR316W-A': 13, 'YDR316W-B': 139, 'YKL083W': 25, 'YOR009W': 25, 'YKL029C': 395, 'YPL166W': 31, 'YKL052C': 20, 'YOL034W': 29, 'YBL008W': 42}

I would like to output this dictionary into Excel. Excel takes up tab-delimited file format. Another way I can go about it is to create two lists and get rid of single quote around 'key'. Any help is much appreciated. Thank you!

1
  • So what is the expected output, one row for each key-value pair? Commented Feb 23, 2016 at 18:07

2 Answers 2

1

Use the csv module to produce Excel-acceptable output. The default dialect is Excel compatible using commas, but you can also switch to a tab-delimited dialect.

Outputing the dictionary as key-value pairs:

import csv

with open(outputfilename, 'wb') as outfile:
    writer = csv.writer(outfile)
    # to get tabs use csv.writer(outfile, dialect='excel-tab')
    writer.writerows(your_dictionary.iteritems())
Sign up to request clarification or add additional context in comments.

Comments

1

You could use the really nice library: xlsxwriter - http://xlsxwriter.readthedocs.org/index.html and just create the Excel very neatly.

--

You could also use pandas and its ExcelWriter function. The basic idea is:

from pandas import DataFrame, ExcelWriter

myDF = DataFrame(myDictionary)
writer = ExcelWriter(someFileName)
myDF.to_excel(writer)
writer.save()

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.