简体   繁体   English

Python重定向/将输出写入文件?

[英]Python redirect/write output to file?

I want to write the output write the output in a file, but all I got was "None" even for words with synonyms. 我想写输出,将输出写到一个文件中,但是即使是带有同义词的单词,我得到的也只是“无”。

Note: when I am not writing in the file the output it works perfectly fine Another note: the output appears on the screen whether i am writing to a file or not, but I get "None" in the file, is theres anyway to fix it. 注意:当我不在文件中写入时,输出效果会很好。另一个注意:无论我是否正在写入文件,输出都会显示在屏幕上,但是我在文件中看到“ None”,是否有需要解决的问题它。 [im using python V2.7 Mac version] [即时通讯使用python V2.7 Mac版本]

file=open("INPUT.txt","w") #opening a file
for xxx in Diacritics:
    print xxx
    synsets = wn.get_synsetids_from_word(xxx) or []
    for s in synsets:
          file.write(str(wn._items[s].describe()))

I tried to simplify the question and rewrite your code so that its an independent test that you should be able to run and eventually modify if that was the problem with your code. 我试图简化问题并重写您的代码,以便它是一项独立的测试,您应该能够运行该测试,并最终进行修改(如果这是代码的问题)。

test = "Is this a real life? Is this fantasy? Caught in a test slide..."
with open('test.txt', 'w') as f:
    for word in test.split():
        f.write(word) # test.txt output: Isthisareallife?Isthisfantasy?Caughtinatestslide...

A side note it almost sounds like you want to append rather than truncate, but I am not sure, so take a look at this . 一个侧面说明它几乎听起来像是要追加,而不是截断,但我不知道,所以来看看这个

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). 最常用的mode值是'r'表示读取,'w'表示写入(如果文件已经存在,则将其截断),以及'a'表示追加(在某些Unix系统上,这意味着所有写入均会追加到末尾)不论当前搜索位置如何)。 If mode is omitted, it defaults to 'r'. 如果省略mode,则默认为'r'。 The default is to use text mode, which may convert '\\n' characters to a platform-specific representation on writing and back on reading. 默认设置为使用文本模式,该模式可以在书写时将'\\ n'字符转换为特定于平台的表示形式,并在读取时将其转换为特定的表示形式。 Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. 因此,打开二进制文件时,应在模式值后附加“ b”以二进制模式打开文件,这将提高可移植性。 (Appending 'b' is useful even on systems that don't treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode. (即使在对二进制文件和文本文件没有不同对待的系统上,追加'b'还是有用的,它作为文档。)请参见下面的mode的更多可能值。

file.write() is going to write whatever is returned by the describe() command. file.write()将要写入describe()命令返回的任何内容。 Because 'None' is being written, and because output always goes to the screen, the problem is that describe is writing to the screen directly (probably with print ) and returning None . 因为正在编写'None',并且因为输出总是进入屏幕,所以问题在于describe是直接写到屏幕(可能是print )并返回None

You need to use some other method besides describe , or give the correct parameters to describe to have it return the strings instead of printing them, or file a bug report. 除了describe之外,您还需要使用其他方法,或者提供正确的参数来描述,以使其返回字符串而不是打印字符串,或者提交错误报告。 (I am not familiar with that package, so I don't know which is the correct course of action.) (我对该程序包不熟悉,所以我不知道哪种方法是正确的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM