English 中文(简体)
以 python [重复] 结尾的线条
原标题:Line endings in python [duplicate]
  • 时间:2012-05-28 13:02:42
  •  标签:
  • python
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Handling vs newlines in python on Mac vs Windows

I m a little bit confused by something, and I m wondering if this is a python thing. I have a text file that uses Windows line endings (" "), but if I iterate through some of the lines in the file, store them in a list, and print out the string representation of the list to the console, it shows " " line endings. Am I missing something?

最佳回答

是的, 它是一个 python 事项; 默认情况下 < code> Open () 打开文本模式的文件, 其中行尾的翻译取决于您代码运行的平台。 您可以在 < code> Open () 调用中设置 < code> newline= , 要求通过未修改的行尾通过 。

Python 2 s 标准 Open () 函数不支持此选项, 只有以二进制模式打开才能防止翻译, 但是您可以通过使用 io. Open () 来使用 Python 3 行为 。

http://docs.python.org/library/formations.html#open” rel=“noreferrer”>关于open 的文件:http://docs.python.org/library/forms.html#open>:

newline 控制着通用新线模式如何运作(仅适用于文本模式)。

[.]

  • When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in , , or , and these are translated into before being returned to the caller. If it is , universal newlines mode is enabled, but line endings are returned to the caller untranslated.
问题回答

以二进制模式打开文件将会在 Windows 上的 Py2 中避免发生这种情况。 但是, 在 Py3 (和 Py2.6+ 中, 如果您使用 io. Open 而不是内建文件) 中, 二进制模式与文本模式之间意味着定义明确且平台独立的事物, 并且不会影响通用的新行 。 相反, 您可以做到 :

file = open(filename,  r , newline=  )

新的线条不会正常化

您应该做的是以通用新线支持打开文件( Python 2.x ) 。 这是使用“ U” 或“ rU” 模式完成的。 任何类型的新线都会得到支持。 以下文件载于 Python 手册< a href=' http://docs. python.org/library/works. html#open” rel = “ noreferrer” > http://docs. python.org/library/ offices.html#open :

In addition to the standard fopen() values mode may be U or rU . Python is usually built with universal newline support; supplying U opens the file as a text file, but lines may be terminated by any of the following: the Unix end-of-line convention , the Macintosh convention , or the Windows convention . All of these external representations are seen as by the Python program. If Python is built without universal newline support a mode with U is the same as normal text mode. Note that file objects so opened also have an attribute called newlines which has a value of None (if no newlines have yet been seen), , , , or a tuple containing all the newline types seen.

对 Python 3 来说,有一个可以打开控制新线行为的新线选项。 查看文档, 似乎通用新线支持是默认的 。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...