问题:将行写入文件的正确方法?

我已经习惯了 print >>f, "hi there"

但是,似乎print >>已经弃用了。推荐使用哪种方法进行上述操作?

更新:关于…的所有这些答案,"\n"这是通用的还是特定于Unix的?IE,我应该"\r\n"在Windows上运行吗?

I’m used to doing print >>f, "hi there"

However, it seems that print >> is getting deprecated. What is the recommended way to do the line above?

Update: Regarding all those answers with "\n"…is this universal or Unix-specific? IE, should I be doing "\r\n" on Windows?


回答 0

这应该很简单:

with open('somefile.txt', 'a') as the_file:
    the_file.write('Hello\n')

从文档:

os.linesep写入以文本模式打开的文件时(默认),请勿用作行终止符;在所有平台上都使用一个’\ n’代替。

一些有用的读物​​:

This should be as simple as:

with open('somefile.txt', 'a') as the_file:
    the_file.write('Hello\n')

From The Documentation:

Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single ‘\n’ instead, on all platforms.

Some useful reading:


回答 1

您应该使用print()Python 2.6+以上版本提供的功能

from __future__ import print_function  # Only needed for Python 2
print("hi there", file=f)

对于Python 3,您不需要import,因为该 print()功能是默认设置。

替代方法是使用:

f = open('myfile', 'w')
f.write('hi there\n')  # python will convert \n to os.linesep
f.close()  # you can omit in most cases as the destructor will call it

引用Python文档中有关换行符的内容:

在输出中,如果换行符为None,则所有'\n'写入的字符都会转换为系统默认的行分隔符os.linesep。如果newline是'',则不会进行翻译。如果换行符是其他任何合法值,'\n'则将写入的所有字符转换为给定的字符串。

You should use the print() function which is available since Python 2.6+

from __future__ import print_function  # Only needed for Python 2
print("hi there", file=f)

For Python 3 you don’t need the import, since the print() function is the default.

The alternative would be to use:

f = open('myfile', 'w')
f.write('hi there\n')  # python will convert \n to os.linesep
f.close()  # you can omit in most cases as the destructor will call it

Quoting from Python documentation regarding newlines:

On output, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.


回答 2

Python文档建议是这样的:

with open('file_to_write', 'w') as f:
    f.write('file contents\n')

所以这就是我通常的方式:)

来自docs.python.org的声明:

在处理文件对象时,最好使用‘with’关键字。这样做的好处是,即使在执行过程中引发了异常,文件在其套件完成后也将正确关闭。它也比编写等效的try-finally块短得多。

The python docs recommend this way:

with open('file_to_write', 'w') as f:
    f.write('file contents\n')

So this is the way I usually do it :)

Statement from docs.python.org:

It is good practice to use the ‘with’ keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks.


回答 3

关于os.linesep:

这是Windows上未经编辑的Python 2.7.1解释器的确切会话:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.linesep
'\r\n'
>>> f = open('myfile','w')
>>> f.write('hi there\n')
>>> f.write('hi there' + os.linesep) # same result as previous line ?????????
>>> f.close()
>>> open('myfile', 'rb').read()
'hi there\r\nhi there\r\r\n'
>>>

在Windows上:

正如预期的那样,os.linesep确实产生相同的结果一样'\n'。它不可能产生相同的结果。'hi there' + os.linesep等同于'hi there\r\n'等同于'hi there\n'

就是这么简单:使用\n它将自动转换为os.linesep。自从将Python首次移植到Windows以来,事情就变得如此简单。

在非Windows系统上使用os.linesep是没有意义的,并且在Windows上会产生错误的结果。

请勿使用os.linesep!

Regarding os.linesep:

Here is an exact unedited Python 2.7.1 interpreter session on Windows:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.linesep
'\r\n'
>>> f = open('myfile','w')
>>> f.write('hi there\n')
>>> f.write('hi there' + os.linesep) # same result as previous line ?????????
>>> f.close()
>>> open('myfile', 'rb').read()
'hi there\r\nhi there\r\r\n'
>>>

On Windows:

As expected, os.linesep does NOT produce the same outcome as '\n'. There is no way that it could produce the same outcome. 'hi there' + os.linesep is equivalent to 'hi there\r\n', which is NOT equivalent to 'hi there\n'.

It’s this simple: use \n which will be translated automatically to os.linesep. And it’s been that simple ever since the first port of Python to Windows.

There is no point in using os.linesep on non-Windows systems, and it produces wrong results on Windows.

DO NOT USE os.linesep!


回答 4

我认为没有“正确”的方法。

我会用:

with open ('myfile', 'a') as f: f.write ('hi there\n')

在纪念蒂姆·托迪(Tim Toady)中

I do not think there is a “correct” way.

I would use:

with open ('myfile', 'a') as f: f.write ('hi there\n')

In memoriam Tim Toady.


回答 5

在Python 3中,它是一个函数,但是在Python 2中,您可以将其添加到源文件的顶部:

from __future__ import print_function

那你做

print("hi there", file=f)

In Python 3 it is a function, but in Python 2 you can add this to the top of the source file:

from __future__ import print_function

Then you do

print("hi there", file=f)

回答 6

如果您要写入大量数据,并且速度是一个问题,那么您可能应该考虑一下f.write(...)。我进行了快速的速度比较,它比print(..., file=f)执行大量写操作时要快得多。

import time    

start = start = time.time()
with open("test.txt", 'w') as f:
    for i in range(10000000):
        # print('This is a speed test', file=f)
        # f.write('This is a speed test\n')
end = time.time()
print(end - start)

write我的机器上,平均完成print时间为2.45秒,而耗时则为(9.76s)的4倍。话虽这么说,在大多数现实情况下这都不是问题。

如果您选择使用,print(..., file=f)您可能会发现您可能会不希望取消换行符,或将其替换为其他内容。这可以通过设置可选end参数来完成,例如;

with open("test", 'w') as f:
    print('Foo1,', file=f, end='')
    print('Foo2,', file=f, end='')
    print('Foo3', file=f)

我建议您使用哪种选择,with因为它使代码更易于阅读。

更新:这种性能差异是由以下事实解释的:write高度缓冲并在实际对磁盘进行任何写操作之前返回(请参阅此答案),而print(可能)使用行缓冲。一个简单的测试就是检查长写的性能,因为行缓冲的缺点(在速度方面)不太明显。

start = start = time.time()
long_line = 'This is a speed test' * 100
with open("test.txt", 'w') as f:
    for i in range(1000000):
        # print(long_line, file=f)
        # f.write(long_line + '\n')
end = time.time()

print(end - start, "s")

现在,性能差异变得不那么明显了,的平均时间为2.20s write和3.10s print。如果您需要连接一串字符串来获得这种良好的性能,那么性能会受到影响,因此使用案例中print效率更高的情况很少见。

If you are writing a lot of data and speed is a concern you should probably go with f.write(...). I did a quick speed comparison and it was considerably faster than print(..., file=f) when performing a large number of writes.

import time    

start = start = time.time()
with open("test.txt", 'w') as f:
    for i in range(10000000):
        # print('This is a speed test', file=f)
        # f.write('This is a speed test\n')
end = time.time()
print(end - start)

On average write finished in 2.45s on my machine, whereas print took about 4 times as long (9.76s). That being said, in most real-world scenarios this will not be an issue.

If you choose to go with print(..., file=f) you will probably find that you’ll want to suppress the newline from time to time, or replace it with something else. This can be done by setting the optional end parameter, e.g.;

with open("test", 'w') as f:
    print('Foo1,', file=f, end='')
    print('Foo2,', file=f, end='')
    print('Foo3', file=f)

Whichever way you choose I’d suggest using with since it makes the code much easier to read.

Update: This difference in performance is explained by the fact that write is highly buffered and returns before any writes to disk actually take place (see this answer), whereas print (probably) uses line buffering. A simple test for this would be to check performance for long writes as well, where the disadvantages (in terms of speed) for line buffering would be less pronounced.

start = start = time.time()
long_line = 'This is a speed test' * 100
with open("test.txt", 'w') as f:
    for i in range(1000000):
        # print(long_line, file=f)
        # f.write(long_line + '\n')
end = time.time()

print(end - start, "s")

The performance difference now becomes much less pronounced, with an average time of 2.20s for write and 3.10s for print. If you need to concatenate a bunch of strings to get this loooong line performance will suffer, so use-cases where print would be more efficient are a bit rare.


回答 7

从3.5开始,您还可以使用pathlib

Path.write_text(data, encoding=None, errors=None)

打开以文本模式指向的文件,向其中写入数据,然后关闭文件:

import pathlib

pathlib.Path('textfile.txt').write_text('content')

Since 3.5 you can also use the pathlib for that purpose:

Path.write_text(data, encoding=None, errors=None)

Open the file pointed to in text mode, write data to it, and close the file:

import pathlib

pathlib.Path('textfile.txt').write_text('content')

回答 8

当您说Line时,它表示一些序列化的字符,它们以’\ n’字符结尾。行应该在最后一点,所以我们应该在每行的末尾考虑’\ n’。这是解决方案:

with open('YOURFILE.txt', 'a') as the_file:
    the_file.write("Hello")

在追加模式下,每次写入光标后移至新行,如果要使用w模式,则应\nwrite()函数末尾添加字符:

the_file.write("Hello\n")

When you said Line it means some serialized characters which are ended to ‘\n’ characters. Line should be last at some point so we should consider ‘\n’ at the end of each line. Here is solution:

with open('YOURFILE.txt', 'a') as the_file:
    the_file.write("Hello")

in append mode after each write the cursor move to new line, if you want to use w mode you should add \n characters at the end of the write() function:

the_file.write("Hello\n")

回答 9

也可以按以下方式使用该io模块:

import io
my_string = "hi there"

with io.open("output_file.txt", mode='w', encoding='utf-8') as f:
    f.write(my_string)

One can also use the io module as in:

import io
my_string = "hi there"

with io.open("output_file.txt", mode='w', encoding='utf-8') as f:
    f.write(my_string)

回答 10

可以使用烧瓶中的文件写入文本:

filehandle = open("text.txt", "w")
filebuffer = ["hi","welcome","yes yes welcome"]
filehandle.writelines(filebuffer)
filehandle.close()

To write text in a file in the flask can be used:

filehandle = open("text.txt", "w")
filebuffer = ["hi","welcome","yes yes welcome"]
filehandle.writelines(filebuffer)
filehandle.close()

回答 11

您也可以尝试 filewriter

pip install filewriter

from filewriter import Writer

Writer(filename='my_file', ext='txt') << ["row 1 hi there", "row 2"]

写入 my_file.txt

接受可迭代对象或带有__str__支持的对象。

You can also try filewriter

pip install filewriter

from filewriter import Writer

Writer(filename='my_file', ext='txt') << ["row 1 hi there", "row 2"]

Writes into my_file.txt

Takes an iterable or an object with __str__ support.


回答 12

当我需要写很多新行时,我定义一个使用print函数的lambda :

out = open(file_name, 'w')
fwl = lambda *x, **y: print(*x, **y, file=out) # FileWriteLine
fwl('Hi')

这种方法的好处是可以利用该功能可用的所有print功能。

更新:正如Georgy在评论部分中提到的那样,可以通过以下partial功能进一步改善此想法:

from functools import partial
fwl = partial(print, file=out)

恕我直言,这是一种功能更强,含糊不清的方法。

When I need to write new lines a lot, I define a lambda that uses a print function:

out = open(file_name, 'w')
fwl = lambda *x, **y: print(*x, **y, file=out) # FileWriteLine
fwl('Hi')

This approach has the benefit that it can utilize all the features that are available with the print function.

Update: As is mentioned by Georgy in the comment section, it is possible to improve this idea further with the partial function:

from functools import partial
fwl = partial(print, file=out)

IMHO, this is a more functional and less cryptic approach.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。