Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'removes': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'way:': 0.09; 'cheers': 0.12; 'collections': 0.16; 'columns': 0.16; 'defaultdict': 0.16; 'executed.': 0.16; 'line.split()': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'whitespace.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'import': 0.22; 'header:User- Agent:1': 0.23; 'planned': 0.24; 'file.': 0.24; 'script': 0.25; 'header:X-Complaints-To:1': 0.27; 'array': 0.29; 'code': 0.31; 'that.': 0.31; 'decimal': 0.31; 'extract': 0.31; 'subject:some': 0.31; 'file': 0.32; 'probably': 0.32; 'another': 0.32; 'open': 0.33; 'subject:the': 0.34; 'could': 0.34; 'test': 0.35; 'but': 0.35; 'method': 0.36; 'should': 0.36; 'wrong': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'according': 0.40; 'remove': 0.60; 'skip:o 30': 0.61; 'here:': 0.62; 'spot': 0.65; 'here': 0.66; 'therefore': 0.72; 'claire': 0.84; 'dict,': 0.84; 'unclear': 0.84; 'apparent': 0.91; 'mistake': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: I used defaultdic to store some variables but the output is blank Date: Sun, 09 Jun 2013 13:56:41 +0200 Organization: None References: <35dd1554-de27-4209-b62e-6a2968c19d0c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084bc0a.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370779004 news.xs4all.nl 15990 [2001:888:2000:d::a6]:39225 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47452 claire morandin wrote: > I have the following script which does not return anything, no apparent > mistake but my output file is empty.I am just trying to extract some > decimal number from a file according to their names which are in another > file. from collections import defaultdict import numpy as np > > [code]ercc_contigs= {} > for line in open ('Faq_ERCC_contigs_name.txt'): > gene = line.strip().split() You probably planned to use the loop above to populate the ercc_contigs dict, but there's no code for that. > ercc_rpkm = defaultdict(lambda: np.zeros(1, dtype=float)) > output_file = open('out.txt','w') > > rpkm_file = open('RSEM_Faq_Q1.genes.results.txt') > rpkm_file.readline() > for line in rpkm_file: > line = line.strip() > columns = line.strip().split() > gene = columns[0].strip() > rpkm_value = float(columns[6].strip()) Remember that ercc_contigs is empty; therefore the test > if gene in ercc_contigs: always fails and the following line is never executed. > ercc_rpkm[gene] += rpkm_value > > ercc_fh = open ('out.txt','w') > for gene, rpkm_value in ercc_rpkm.iteritems(): > ercc = '{0}\t{1}\n'.format(gene, rpkm_value) > ercc_fh.write (ercc)[/code] > > If someone could help me spot what's wrong it would be much appreciate > cheers By the way: it is unclear to my why you are using a numpy array here: > ercc_rpkm = defaultdict(lambda: np.zeros(1, dtype=float)) I think ercc_rpkm = defaultdict(float) should suffice. Also: > line = line.strip() > columns = line.strip().split() > gene = columns[0].strip() > rpkm_value = float(columns[6].strip()) You can remove all strip() method calls here as line.split() implicitly removes all whitespace.