ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

译(二十九)-Python按行读取文件并存进列表

2021-10-09 13:33:04  阅读:207  来源: 互联网

标签:读取 Python lines filename 按行 file line open


文章首发及后续更新:https://mwhls.top/3093.html
新的更新内容请到mwhls.top查看。
无图/无目录/格式错误/更多相关请到上方的文章首发页面查看。

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

目录 1. 如何按行读取文件并存进列表? 2. How to read a file line-by-line into a list?

如何按行读取文件并存进列表?

  • Julie Raswick asked:

    • Python如何读取一个文件的每一行,并将其存进列表里?
    • 按行读取,然后追加到列表末尾。
  • Answers:

    • SilentGhost – vote: 2450

    • 下面的代码可以读取文件至内存,并且移除每行的空白字符(换行符与空格):

    • with open(filename) as file:
      lines = file.readlines()`
      lines = [line.rstrip() for line in lines]

  • 如果是读取大文件,最后按行读取并处理:

  • with open(filename) as file:
      for line in file:    
          print(line.rstrip())
  • Python3.8可以用带海象运算符的while循环(译者注::=长得像海象,所以这么叫,文档里说的):

  • with open(filename) as file:
     while (line := file.readline().rstrip()):    
         print(line)```
  • 根据文件的处理需求与编码方式来设置不同的读取方式与编码方式:

  • with open(filename, 'r', encoding='UTF-8') as file:
     while (line := file.readline().rstrip()):  
         print(line)```
  • Felix Kling – vote: 1108

  • 输入与输出

  • with open('filename') as f:
     lines = f.readlines()```
  • 或者去除换行符:

  • with open('filename') as f:
      lines = [line.rstrip() for line in f]```
  • robert – vote: 667

  • 虽然这不简洁,但的确如要求一般:

  • with open(file.txt) as file_in:
     lines = []  
     for line in file_in:    
         lines.append(line)```

  • How to read a file line-by-line into a list?

    • Julie Raswick asked:

      • How do I read every line of a file in Python and store each line as an element in a list?
        Python如何读取一个文件的每一行,并将其存进列表里?
      • I want to read the file line by line and append each line to the end of the list.
        按行读取,然后追加到列表末尾。
    • Answers:

      • SilentGhost – vote: 2450

      • This code will read the entire file into memory and remove all whitespace characters (newlines and spaces) from the end of each line:
        下面的代码可以读取文件至内存,并且移除每行的空白字符(换行符与空格):

      • with open(filename) as file:
          lines = file.readlines()`    
          lines = [line.rstrip() for line in lines]
      • If you\’re working with a large file, then you should instead read and process it line-by-line:
        如果是读取大文件,最后按行读取并处理:

      • with open(filename) as file:
          for line in file:    
              print(line.rstrip())
      • In Python 3.8 and up you can use a while loop with the walrus operator like so:
        Python3.8可以用带海象运算符的while循环(译者注::=长得像海象,所以这么叫,文档里说的):

      • with open(filename) as file:
         while (line := file.readline().rstrip()):    
             print(line)```
      • Depending on what you plan to do with your file and how it was encoded, you may also want to manually set the access mode and character encoding:
        根据文件的处理需求与编码方式来设置不同的读取方式与编码方式:

      • with open(filename, 'r', encoding='UTF-8') as file:
         while (line := file.readline().rstrip()):  
             print(line)```
      • Felix Kling – vote: 1108

      • See Input and Ouput:
        输入与输出

      • with open('filename') as f:
         lines = f.readlines()```
      • or with stripping the newline character:
        或者去除换行符:

      • with open('filename') as f:
          lines = [line.rstrip() for line in f]```
      • robert – vote: 667

      • This is more explicit than necessary, but does what you want.
        虽然这不简洁,但的确如要求一般:

      • with open(file.txt) as file_in:
         lines = []  
         for line in file_in:    
             lines.append(line)```

标签:读取,Python,lines,filename,按行,file,line,open
来源: https://blog.csdn.net/asd123pwj/article/details/120669135

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有