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

      1. <tfoot id='qqfp4'></tfoot>

        <legend id='qqfp4'><style id='qqfp4'><dir id='qqfp4'><q id='qqfp4'></q></dir></style></legend>
          <bdo id='qqfp4'></bdo><ul id='qqfp4'></ul>

        在 Python 中将行号添加到输出中

        时间:2023-06-06

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

              <tbody id='9TBPt'></tbody>
            • <bdo id='9TBPt'></bdo><ul id='9TBPt'></ul>

                <tfoot id='9TBPt'></tfoot>

                  <small id='9TBPt'></small><noframes id='9TBPt'>

                • 本文介绍了在 Python 中将行号添加到输出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  例如,如果输入文件是:

                  For example, if the input file is:

                  def main():
                      for i in range(10):
                          print("I love Python")
                      print("Good bye!")
                  

                  那么输出将是:

                  1   def main():
                  2       for i in range(10):
                  3           print("I love Python")
                  4       print("Good bye!")
                  

                  我很难在每行中添加行.我的程序是:

                  I have difficulty in adding lines to each line. My program is:

                  filename = input("Please enter a file name: ")
                  count = 0
                  
                  openfile = open(filename, "r")
                  
                  for lines in openfile:
                      linenumbers = openfile.write(str(count)+'	'+lines)
                      count += 1
                  
                  print(count)
                  

                  推荐答案

                  使用 with 语句关闭文件缓冲区,只连接字符串:

                  Use a with statement to close the file buffer and just concatenate strings:

                  with open('file.txt', 'r') as program:
                      data = program.readlines()
                  
                  with open('file.txt', 'w') as program:
                      for (number, line) in enumerate(data):
                          program.write('%d  %s' % (number + 1, line))
                  

                  这篇关于在 Python 中将行号添加到输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将两个列表加入格式化字符串的最聪明方法 下一篇:Python dateutil.parser.parse 首先解析月份,而不是日期

                  相关文章

                    <bdo id='K2E1i'></bdo><ul id='K2E1i'></ul>

                  <small id='K2E1i'></small><noframes id='K2E1i'>

                • <i id='K2E1i'><tr id='K2E1i'><dt id='K2E1i'><q id='K2E1i'><span id='K2E1i'><b id='K2E1i'><form id='K2E1i'><ins id='K2E1i'></ins><ul id='K2E1i'></ul><sub id='K2E1i'></sub></form><legend id='K2E1i'></legend><bdo id='K2E1i'><pre id='K2E1i'><center id='K2E1i'></center></pre></bdo></b><th id='K2E1i'></th></span></q></dt></tr></i><div id='K2E1i'><tfoot id='K2E1i'></tfoot><dl id='K2E1i'><fieldset id='K2E1i'></fieldset></dl></div>

                • <tfoot id='K2E1i'></tfoot>
                    1. <legend id='K2E1i'><style id='K2E1i'><dir id='K2E1i'><q id='K2E1i'></q></dir></style></legend>