• <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 中将行号添加到输出中

        Adding line numbers to the output in Python(在 Python 中将行号添加到输出中)

        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 中将行号添加到输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                      <bdo id='K2E1i'></bdo><ul id='K2E1i'></ul>

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

                        <tbody id='K2E1i'></tbody>
                      • <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>