0 Replies - 97 Views - Last Post: 25 October 2013 - 05:52 AM Rate Topic: -----

#1 ow2914  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-October 13

Problem using glob in my Python code.

Posted 25 October 2013 - 05:52 AM

Can someone please tell me what is wrong with my code.
It does NOT give and error. But it also does not produce an output.

The script is made to do the following:

The script takes an input file of 4 tab-separated columns:

It then counts the unique values in Column 1 and the frequency of corresponding values in Column 4 (which contains 2 different tags: C and D).

The output is 3 tab-separated columns containing the unique values of column 1 and their corresponding frequency of values in Column 4: Column 2 has the frequency of the string in Column 1 that corresponds with Tag C and Column 3 has the frequency of the string in Column 1 that corresponds with Tag D.

Here is a sample of input:
algorithm-n   like-1-resonator-n   8.1848   C
algorithm-n   produce-hull-n   7.9104   C
algorithm-n   like-1-resonator-n   8.1848   D
algorithm-n   produce-hull-n   7.9104   D
anything-n   about-1-Zulus-n   7.3731   C
anything-n   above-shortage-n   6.0142   C
anything-n   above-1-gig-n   5.8967   C
anything-n   above-1-magnification-n   7.8973   C
anything-n   after-1-memory-n   2.5866   C

Here is a sample of desired output:
algorithm-n   2   2
anything-n      5   0

The code that I have done is the following:

from collections import defaultdict, Counter 

def sortAndCount(opened_file):
    lemma_sense_freqs = defaultdict(Counter)    
    for line in opened_file:
        lemma, _, _, senseCode = line.split()
        lemma_sense_freqs[lemma][senseCode] += 1
    return lemma_sense_freqs
def writeOutCsv(output_file, input_dict):
    with open(output_file, "wb") as outfile:
        for lemma in input_dict.keys():
            for senseCode in input_dict[lemma].keys():
                outstring = "\t".join([lemma, senseCode,\
                outfile.write(outstring + "\n")

import os
import glob

folderPath = "Python_Counter" # declare here

for input_file in glob.glob(os.path.join(folderPath, 'out_')):
	with open(input_file, "rb") as opened_file:
		lemma_sense_freqs = sortAndCount(input_file)
	output_file = "count_*.csv"
	writeOutCsv(output_file, lemma_sense_freqs)

My intuition is the problem is coming from the "glob" function.
But, as I said before: the code itself DOES NOT give me an error -- but it doesn't seem to produce an output either.

Can someone help?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1