简体   繁体   English

一次用Python读取一行文件

[英]Reading file in Python one line at a time

I do appreciate this question has been asked million of time, but I can't figure out while attempting to read a .txt file line by line I get the entire file read in one go. 我确实很欣赏这个问题已被问了无数次,但尝试逐行读取.txt文件时却无法弄清楚,我一口气读取了整个文件。

This is my little snippet 这是我的小片段

    num = 0

with open(inStream, "r") as f:
    for line in f:
        num += 1
        print line + " ..."
        print num

Having a look at the open function there is anything that suggest a second param to limit the reading as that is just the "mode" to pen the file. 看一下open函数,有什么东西暗示了第二个参数来限制读取,因为这仅仅是笔的“模式”。

So I can only guess there are same problem with my file, but this is a txt file, with entry line by line. 因此,我只能猜测文件存在相同的问题,但这是一个txt文件,逐行输入。

Any hint? 有什么提示吗?

Without a little more information, it's hard to be absolutely sure… but most likely, your problem is inappropriate line endings. 没有更多的信息,很难绝对确定……但是很可能您的问题是不合适的行尾。


For example, on a modern Mac OS X system, lines in text files end with '\\n' newline characters. 例如,在现代Mac OS X系统上,文本文件中'\\n'行以'\\n'换行符结尾。 So, when you do for line in f: , Python breaks the text file on '\\n' characters. 因此,当您for line in f: ,Python会使用'\\n'字符将文本文件断开。

But on classic Mac OS 9, lines in text files ended with '\\r' instead. 但是在经典Mac OS 9上,文本文件中的行以'\\r'结尾。 If you have some ancient classic Mac text files lying around, and you give one to Python, it will go looking for '\\n' characters and not find any, so it'll think the whole file is one giant line. 如果周围有一些古老的经典Mac文本文件,并且给Python一个文件,它将寻找'\\n'字符而找不到任何字符,因此它将认为整个文件是一个巨大的一行。

(Of course in real life, Windows is a problem more often than classic Mac OS, but I used this example because it's simpler.) (当然,在现实生活中,Windows比经典Mac OS经常出现问题,但我使用了此示例,因为它更简单。)


Python 2 : Fortunately, Python has a feature called "universal newlines" . Python 2 :幸运的是,Python具有一个称为“通用换行符”的功能 For full details, see the link, but the short version is that adding "U" onto the end of the mode when opening a text file means Python will read any of the three standard line-ending conventions (and give them to your code as Unix-style '\\n' ). 有关完整的详细信息,请参见链接,但是简短的版本是在打开文本文件时在模式的末尾添加"U"表示Python将读取三种标准的行尾约定(并将它们作为代码提供给您) Unix风格的'\\n' )。

In other words, just change one line: 换句话说,只需更改一行:

with open(inStream, "rU") as f:

Python 3 : Universal newlines are part of the standard behavior; Python 3 :通用换行符是标准行为的一部分; adding "U" has no effect and is deprecated. 添加"U"无效,不建议使用。

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

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