简体   繁体   中英

How to format the output of dictionary in python

I would like to format the values of a dictionary in python. Here is the script that i have used to generate the output

entries = {}
entries1 = {}

with open('no_dup.txt', 'r') as fh_in:
    for line in fh_in:
        if line.startswith('E'):
            line = line.strip()
            line = line.split()
            entry = line[0]
            if entry in entries:
                entries[entry].append(line)
            else:
                entries[entry] = [line]

with open('no_dup_out.txt', 'w') as fh_out:
    for kee, val in entries.iteritems():
        if len(val) == 1:
            fh_out.write("{} \n".format(val))


with open('no_dup_out.txt', 'r') as fh_in2:
    for line in fh_in2:
        line = line.strip()
        line = line.split()
        entry = line[1]
        if entry in entries1:
            entries1[entry].append(line)
        else:
            entries1[entry] = [line]


with open('no_dup_out_final.txt', 'w') as fh_out2:
    for kee, val in entries1.iteritems():
        if len(val) == 1:
            fh_out2.write("{} \n".format(val))

For example by running the above script i generated the following output

[["[['ENSGMOG00000003747',", "'ENSORLG00000006947']]"]] 
[["[['ENSGMOG00000003752',", "'ENSORLG00000005385']]"]] 
[["[['ENSGMOG00000003760',", "'ENSORLG00000005379']]"]] 
[["[['ENSGMOG00000003748',", "'ENSORLG00000004636']]"]] 
[["[['ENSGMOG00000003761',", "'ENSORLG00000005382']]"]] 

And i would like to format it such as way that i remove all the parentheses and commas ( ENSGMOG00000003747 ENSORLG00000006947 ) and output the rest as it is using tab delimited format. How can i do that?

如果列表列表为full_list ,则可以使以下代码给出所需的输出:

desired_list = ['\t'.join([element.split('\'')[1] for element in list_item[0]]) for list_item in full_list]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM