views:

151

answers:

4

When writing to a text file, some of the file.write instances are followed by a linebreak in the output file and others aren't. I don't want linebreaks except where I tell them to occur. Code:

 for doc,wc in wordcounts.items(): 
     out.write(doc)             #this works fine, no linebreak
     for word in wordlist: 
         if word in wc: out.write("\t%d" % wc[word]) #linebreaks appear
         else: out.write("\t0")                      #after each of these
     out.write("\n")        #this line had mixed spaces/tabs

What am I missing?

Update

I should have taken a clue from how the code pasted into SO. For some reason there was a mixture of spaces and tabs in the final line, such that in TextMate it visually appeared outside the "for word..." loop—but the interpreter was treating it as part of that loop. Converting spaces to tabs solved the problem.

Thanks for your input.

A: 

You write a line breaks after every word:

for word in wordlist:
    ...
    out.write("\n")

Are these the line breaks you are seeing, or are there more additional ones?

sth
+2  A: 

file.write() does not add any newlines if the string you write does not contain any \ns.

But you force a newline for each word in your word list using out.write("\n"), is that what you want?

    for doc,wc in wordcounts.items(): 
        out.write(doc)             #this works fine, no linebreak
        for word in wordlist: 
            if word in wc: out.write("\t%d" % wc[word]) #linebreaks appear
            else: out.write("\t0")                      #after each of these
            out.write("\n") #<--- NEWLINE ON EACH ITERATION!

Perhaps you indented out.write("\n") too far???

Johannes Weiß
No, I don't want a newline on each iteration—I want a newline for each doc, with the words separated by tabs.
Dan
Ok, so your last out.write is intended too far, should be on the same level as the for word in wordlist -loop (i.e. in your question, 4 spaces left)
Kimvais
+1  A: 

You might need to perform a strip() on each wc[word]. Printing a single item from wc is would probably be enough to determine if there are already line breaks on those items that area causing this behavior.

Either that or the indentation on your final out.write("\n") is not doing what you intended it to do.

jathanism
Latter is correct I think, the former is misleading as he is already doing "%d" % wc[word] which suggests that wc[word] are integers, not strings that can be .strip()ped...
Kimvais
A: 

I think your indentation is wrong.

(also I took the liberty to make your if clause redundant and code more readable :)

for doc,wc in wordcounts.items()
   out.write(doc)
   for word in wordlist:
     out.write("\t%d" % wc.get(word,0))
   out.write("\n")
Kimvais