简体   繁体   中英

Python: writing two different dictionaries out to two different csv files

My program looks into a folder of text files and generates counts of two features I am looking at "SS" and "DS" and how many times they occur. So I set up a dictionary that lists the feature and how many times it occurs in the texts.

I want my SS dictionary to write out to one csv file and the DS dictionary to write out to another csv file.

Here is my code so far:

import glob
import re

path = "tagged texts\*.txt"

#establish counts and dictionaries
list_SS = []
list_DS = []
SScounts = {}
DScounts = {}

#generate counts of the features
for file in glob.glob(path):
    line_count = 0
    with open(file, encoding='utf-8', errors='ignore') as file_in:
        text = file_in.readlines()
        for line in text:
            word = line.split('_')
            if word[2] == "SS":
                list_SS.append(word[0])
            elif word[2] == "DS":
                list_DS.append(word[0])

#create dictionary for SS and write out results to file
file1_out = open("SS_counts.csv", "w+")
for w in list_SS:
    SScounts[w] = SScounts.get(w,0) + 1
for i in sorted(SScounts, key = SScounts.get, reverse=True):
    file1_out.write(str(i) + "," + str(SScounts[i]) + "\n")

#create dictionary for DS and write out results to file
file2_out = open ("DS_counts.csv", "w+")
for w in list_DS:
    DScounts[w] = DScounts.get(w,0) + 1
for i in sorted(DScounts, key = DScounts.get, reverse=True):
    file2_out.write(str(i) + "," + str(DScounts[i]) + "\n")

The SS dictionary comes out just fine, this what the results look like in the csv file:

nisha,41
rasha,19
rikusha,13
apisha,11
nishashi,8
...

The problem is that the second file, the DS file turns out blank, there's nothing in it. Before with some fiddling with the variable names in the dictionary I would get the results for the SS dictionary written out to both files.

I created two dictionaries after asking my prof and he said it's possible to do it from one dictionary but it'd be simpler to use two. And I guess I could just write a separate python script for the DS results but I'd like to do both in the same script.

Sooo, what's the deal? Why is the second dictionary not writing out to the second file?

The citizens of StackOverflow cannot run your code. It often helps to create a Minimal, Complete, and Verifiable example that others can run.

A crucial question: do list_SS and list_DS have data in them? Drop all of the code starting with file1_out and instead use this:

assert list_SS
assert list_DS

If those assertions fail, you've narrowed the problem considerably.

Another key question: can you reproduce the problem if you eliminate the globbing and file reading? Roughly like this:

list_SS = []
list_DS = []
SScounts = {}
DScounts = {}

text = [
    'an example line from your data files...',
    'ditto...',
    '...',
]

for line in text:
    word = line.split('_')
    if word[2] == "SS":
        list_SS.append(word[0])
    elif word[2] == "DS":
        list_DS.append(word[0])

assert list_SS
assert list_DS

At this point you'll have something that StackOverflow can help with ... but you might not need us by then.

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