Python Forum
Python help with module function return dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python help with module function return dictionary
#11
(Oct-15-2016, 07:43 PM)Larz60+ Wrote: Hello again,

I would like to take a closer look at how this data is exactly laid out.
Is this from the ncbi blast database? and if so, which file.

I would rather be working with an actual file.

Larz60+

Hello. I dont know actually if its from the ncbi database or not I've got the file from someone else and I dont know where they've got it. :s!

How do you attach or send a file here? I cant seem to find it in here :huh:

(Oct-15-2016, 07:35 PM)wavic Wrote: Does this work? It's not I've proposed. It's step by step. If the file doesn't contain something else...

def get_data(f):
    data = f.read().split()
    ecoli = dict()
    e_name = None

    for row in data:
        if row.startswith(">"):
            e_name = row.strip(">")
            ecoli[e_name] = ""
        else:
            ecoli[e_name] = "{}{}".format(ecoli[e_name], row)

    return ecoli

I got an error for data=f.read().split()  so I tried to change it to data = open(f).read().split()  and I got no error but the output were only the values and also not in a dictionary. :-/
Reply
#12
Probably not a good idea as size could be an issue.
If the name of the file hasn't changed, I should be able to find it.
what name?

here is the blast help file location: https://blast.ncbi.nlm.nih.gov/Blast.cgi...=BlastHelp
Reply
#13
(Oct-15-2016, 08:22 PM)Larz60+ Wrote: Probably not a good idea as size could be an issue.
If the name of the file hasn't changed, I should be able to find it.
what name?

here is the blast help file location: https://blast.ncbi.nlm.nih.gov/Blast.cgi...=BlastHelp

The name of the file is "Ecoli.prot.fasta"
Reply
#14
OK,

This should do the trick. It's not the most efficient code, but you can clean it up. It works, that's the important thing.

def read_fasta(filename=None):
    table_dict = {}
    update_dict = False
    if filename is not None:
        name = ''
        value = ''
        with open(filename, 'r') as f:
            for line in f.readlines():
                line = line.strip()

                if line[0] == ">":
                    if update_dict:
                        table_dict[name] = value
                        value = ''
                    name = line[1:]
                else:
                    update_dict = True
                    if len(line):
                        value += line

            if len(value):
                table_dict[name] = value

        print(table_dict)


if __name__ == '__main__':
    read_fasta('Ecoli.prot.fasta')
Larz60+
Reply
#15
Quote:How do you attach or send a file here? I cant seem to find it in here [Image: huh.png]

I posted only the function which is supposed to do the job.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import pprint

def get_data(f):
    data = f.read().split()
    ecoli = dict()
    e_name = None

    for row in data:
        if row.startswith(">"):
            e_name = row.strip(">")
            ecoli[e_name] = ""
        else:
            ecoli[e_name] = "{}{}".format(ecoli[e_name], row)

    return ecoli

def main():

    with open("ecoli.txt") as in_file:
        pprint.pprint(get_data(in_file))

if __name__ == '__main__':
    sys.exit(main())
Try @Larz60's solution first. He is a real programmer. I code for fun. :surfing:
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 624 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,312 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,193 Feb-04-2023, 12:30 PM
Last Post: caslor
  passing dictionary to the function mark588 2 983 Dec-19-2022, 07:28 PM
Last Post: deanhystad
  return vs. print in nested function example Mark17 4 1,757 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  How to invoke a function with return statement in list comprehension? maiya 4 2,867 Jul-17-2021, 04:30 PM
Last Post: maiya
  Function - Return multiple values tester_V 10 4,470 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Get return value from a threaded function Reverend_Jim 3 17,155 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
  Writing a lambda function that sorts dictionary GJG 1 2,035 Mar-09-2021, 06:44 PM
Last Post: buran
  Using Dictionary to Test Evenness of Distribution Generated by Randint Function new_coder_231013 6 3,304 Feb-23-2021, 01:29 PM
Last Post: new_coder_231013

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020