Problem using glob in my Python code.

Page 1 of 1

0 Replies - 225 Views - Last Post: 25 October 2013 - 05:52 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=332406&amp;s=d84f3a70472dbb48fbb69eade369bf85&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 ow2914

Reputation: 0
• 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   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,\
str(input_dict[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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }