禁止 file.write 上的换行符

发布于 2024-08-13 10:08:55 字数 657 浏览 8 评论 0原文

写入文本文件时,输出文件中的某些 file.write 实例后面会跟有换行符,而其他实例则不会。我想要换行,除非我告诉它们发生的地方。代码:

    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

我缺少什么?

更新

我应该从代码如何粘贴到 SO 中找到线索。由于某种原因,最后一行中混合了空格和制表符,因此在 TextMate 中,它在视觉上出现在“for word...”循环之外,但解释器将其视为那个循环。将空格转换为制表符解决了这个问题。

感谢您的意见。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟酉 2024-08-20 10:08:55

如果您写入的字符串不包含任何 \n,则 file.write() 不会添加任何换行符。

但是您使用 out.write("\n") 为单词列表中的每个单词强制换行,这是您想要的吗?

    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!

也许你缩进 out.write("\n") 太远了???

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???

终陌 2024-08-20 10:08:55

你在每个单词后面写一个换行符:

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

这些是你看到的换行符,还是还有更多的换行符?

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?

小嗷兮 2024-08-20 10:08:55

您可能需要对每个 wc[word] 执行 strip()。从 wc 打印单个项目可能足以确定导致此行为的那些项目上是否已经存在换行符。

或者是您最终的out.write("\n") 上的缩进没有达到您的预期目的。

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.

风渺 2024-08-20 10:08:55

我认为你的缩进是错误的。

(我还冒昧地让你的 if 子句变得多余,并使代码更具可读性:)

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

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文