• <tfoot id='BjtaA'></tfoot>

  • <legend id='BjtaA'><style id='BjtaA'><dir id='BjtaA'><q id='BjtaA'></q></dir></style></legend>
      <i id='BjtaA'><tr id='BjtaA'><dt id='BjtaA'><q id='BjtaA'><span id='BjtaA'><b id='BjtaA'><form id='BjtaA'><ins id='BjtaA'></ins><ul id='BjtaA'></ul><sub id='BjtaA'></sub></form><legend id='BjtaA'></legend><bdo id='BjtaA'><pre id='BjtaA'><center id='BjtaA'></center></pre></bdo></b><th id='BjtaA'></th></span></q></dt></tr></i><div id='BjtaA'><tfoot id='BjtaA'></tfoot><dl id='BjtaA'><fieldset id='BjtaA'></fieldset></dl></div>
        • <bdo id='BjtaA'></bdo><ul id='BjtaA'></ul>
      1. <small id='BjtaA'></small><noframes id='BjtaA'>

      2. Python 中“虽然不是 EOF"的完美对应物是什么?

        What is the perfect counterpart in Python for quot;while not EOFquot;(Python 中“虽然不是 EOF的完美对应物是什么?)
        <i id='0dsUq'><tr id='0dsUq'><dt id='0dsUq'><q id='0dsUq'><span id='0dsUq'><b id='0dsUq'><form id='0dsUq'><ins id='0dsUq'></ins><ul id='0dsUq'></ul><sub id='0dsUq'></sub></form><legend id='0dsUq'></legend><bdo id='0dsUq'><pre id='0dsUq'><center id='0dsUq'></center></pre></bdo></b><th id='0dsUq'></th></span></q></dt></tr></i><div id='0dsUq'><tfoot id='0dsUq'></tfoot><dl id='0dsUq'><fieldset id='0dsUq'></fieldset></dl></div>
          <tbody id='0dsUq'></tbody>

          <bdo id='0dsUq'></bdo><ul id='0dsUq'></ul>

          1. <tfoot id='0dsUq'></tfoot>
            <legend id='0dsUq'><style id='0dsUq'><dir id='0dsUq'><q id='0dsUq'></q></dir></style></legend>

              <small id='0dsUq'></small><noframes id='0dsUq'>

                • 本文介绍了Python 中“虽然不是 EOF"的完美对应物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  要在 C 或 Pascal 中读取一些文本文件,我总是使用以下代码段来读取数据,直到 EOF:

                  To read some text file, in C or Pascal, I always use the following snippets to read the data until EOF:

                  while not eof do begin
                    readline(a);
                    do_something;
                  end;
                  

                  因此,我想知道如何在 Python 中简单快速地做到这一点?

                  Thus, I wonder how can I do this simple and fast in Python?

                  推荐答案

                  遍历文件读取行:

                  with open('somefile') as openfileobject:
                      for line in openfileobject:
                          do_something()
                  

                  文件对象是可迭代的,并且在 EOF 之前产生行.将文件对象用作可迭代对象使用缓冲区来确保高性能读取.

                  File objects are iterable and yield lines until EOF. Using the file object as an iterable uses a buffer to ensure performant reads.

                  你可以对标准输入做同样的事情(不需要使用 raw_input():

                  You can do the same with the stdin (no need to use raw_input():

                  import sys
                  
                  for line in sys.stdin:
                      do_something()
                  

                  为了完成图片,可以通过以下方式完成二进制读取:

                  To complete the picture, binary reads can be done with:

                  from functools import partial
                  
                  with open('somefile', 'rb') as openfileobject:
                      for chunk in iter(partial(openfileobject.read, 1024), b''):
                          do_something()
                  

                  其中 chunk 将包含来自文件的最多 1024 个字节,并且当 openfileobject.read(1024) 开始返回空字节字符串时迭代停止.

                  where chunk will contain up to 1024 bytes at a time from the file, and iteration stops when openfileobject.read(1024) starts returning empty byte strings.

                  这篇关于Python 中“虽然不是 EOF"的完美对应物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                  <legend id='6Z1QH'><style id='6Z1QH'><dir id='6Z1QH'><q id='6Z1QH'></q></dir></style></legend>
                    <tbody id='6Z1QH'></tbody>
                  1. <i id='6Z1QH'><tr id='6Z1QH'><dt id='6Z1QH'><q id='6Z1QH'><span id='6Z1QH'><b id='6Z1QH'><form id='6Z1QH'><ins id='6Z1QH'></ins><ul id='6Z1QH'></ul><sub id='6Z1QH'></sub></form><legend id='6Z1QH'></legend><bdo id='6Z1QH'><pre id='6Z1QH'><center id='6Z1QH'></center></pre></bdo></b><th id='6Z1QH'></th></span></q></dt></tr></i><div id='6Z1QH'><tfoot id='6Z1QH'></tfoot><dl id='6Z1QH'><fieldset id='6Z1QH'></fieldset></dl></div>
                            <bdo id='6Z1QH'></bdo><ul id='6Z1QH'></ul>

                          • <tfoot id='6Z1QH'></tfoot>

                            <small id='6Z1QH'></small><noframes id='6Z1QH'>