0

I have a file that partially looks like this:

{'PLGLB1': array([0, 2, 2]), 'IL20RA': array([2, 0, 0]), 'MORC1': array([2, 0, 0])}...

and I want the output to be in a table-like form:

PLGLB1 0 2 2
IL20RA 2 0 0
MORC1 2 0 0

without any indentation or brackets. How can I do it?

I tried with:

for i in dict:
    print("{}\t{}".format(i,dict[i]))

(it is the continuation of a big script) but I get this output:

BAIAP3  [0 2 2]
CALCA   [0 2 2]
MYT1    [2 0 0]
2
  • 1
    What have you tried? A for loop and a print would go a long way in solving your problem. Commented Apr 12, 2018 at 15:11
  • 2
    How did that file even come to be? That's a horrible data serialization format. You should probably fix that upstream instead of trying to parse that thing. (Just so we're clear, {'PLGLB1': array([0, 2, 2]), 'IL20RA': ... is text in a file, right?) Commented Apr 12, 2018 at 15:11

3 Answers 3

1

With such a dictionary, and the fact that you're trying to put it into a dataframe like structure, I would consider using pandas. It's fast, and you can get your desired frame in one short line (assuming your dictionary is called my_dict):

import pandas as pd

df = pd.DataFrame.from_dict(my_dict, orient='index')

which returns:

        0  1  2
PLGLB1  0  2  2
IL20RA  2  0  0
MORC1   2  0  0

I'm unsure of your final goal, but it seems like you want this to end up being outputted as a text file (correct me if I'm wrong), in which case you can just do this:

df.to_csv('filename.txt', sep=' ', header=False)

Which will make a text file called filname.txt looking like this:

PLGLB1 0 2 2
IL20RA 2 0 0
MORC1 2 0 0

Note: If the outputted .txt is your only goal, and you don't want to work with the dataframe in Python, you can even chain the whole thing without saving the frame as df:

pd.DataFrame.from_dict(my_dict, orient='index').to_csv('filename.txt', sep=' ', header=False)
Sign up to request clarification or add additional context in comments.

Comments

0
dictionary = {'PLGLB1': [0, 2, 2], 'IL20RA': [2, 0, 0], 'MORC1': [2, 0, 0]}

for key in dictionary:
    print(key, *dictionary[key])

Sounds like this is what you're looking for. The star in the print statement removes the brackets from the lists and separates the elements with a space.

Comments

0

I assume that your input is a long string, as shown in the question. No standard parser for this format likely exists. Regular expressions to the rescue.

import re

entry_rx = re.compile(r"'(\w+)': array\(\[(\d+), (\d+), (\d+)\]\)")

def print_group_per_line(input_string):
    for match in entry_rx.finditer(crazy_text):
        print("{0} {1} {2} {3}".format(*match.groups()))

Testing it:

>>> crazy_text = "{'PLGLB1': array([0, 2, 2]), 'IL20RA': array([2, 0, 0]), 'MORC1': array([2, 0, 0])}"
>>> print_group_per_line(crazy_text)
PLGLB1 0 2 2
IL20RA 2 0 0
MORC1 2 0 0

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.